Discussion

Unzipping the File

package com.visa.filereader;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class ZipReader {
// Expands the zip file passed as argument 1, into the
// directory provided in argument 2
public static void main(String args[]) throws Exception {

if (args.length != 2) {
// args[0] = ;
// args[1] = "c:\\execute";
}
InputStream theFile = new FileInputStream("c:\\execute\\<filename>.zip");
ZipInputStream stream = new ZipInputStream(theFile);
// create a buffer to improve copy performance later.

// open the zip file stream

String outdir = "c:\\execute";

try {

// now iterate through each item in the stream. The get next
// entry call will return a ZipEntry for each file in the
// stream
ZipEntry entry;
while ((entry = stream.getNextEntry()) != null) {
byte[] buffer = new byte[stream.available()];
System.out.println(entry.getName());

// Once we get the entry from the stream, the stream is
// positioned read to read the raw data, and we keep
// reading until read returns 0 or less.
if (entry.getName().startsWith("<filename>")) {
String outpath = outdir + "/" + entry.getName() + ".xml";

int len = 0;
String fileContent = null;
StringBuffer content = new StringBuffer();
while ((len = stream.read(buffer)) > 0) {
fileContent = new String(buffer);
content.append(fileContent);
}

getTheXML(content.toString(), outpath);

}
}
} finally {
// we must always close the zip file.
stream.close();
}
}

private static void getTheXML(String xmlcontent, String outpath) {
// System.out.println("xml:" + xmlcontent);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = null;
FileOutputStream output = null;
try {
documentBuilder = documentBuilderFactory.newDocumentBuilder();
try {
Document document = documentBuilder.parse(new InputSource(
new ByteArrayInputStream(xmlcontent.getBytes())));
NodeList list = document.getElementsByTagName("<Spliter>");
System.out.println(list.getLength());
for (int i = 0; i < list.getLength(); i++) {
output = new FileOutputStream(outpath + "_" + i
+ "Test.xml");

StringWriter sw = new StringWriter();
Transformer serializer = null;
try {
serializer = TransformerFactory.newInstance()
.newTransformer();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
serializer.transform(new DOMSource(list.item(i)),
new StreamResult(sw));
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String result = sw.toString();

output.write(result.getBytes());
}
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// we must always close the output file
if (output != null)
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

**Moderation Team has archived post**

This post has been archived for educational purposes. Contents and links will no longer be updated. If you have the same/similar question, please write a new post.

Comments

Keep up to date on this post and subscribe to comments

December 29, 2017 - 6:16am

When ı save this into the function I got this error. Please help me.

 

line: 1
Illegal modifier for the local class ZipReader; only abstract or final is permitted

Library—

The Library failed to compile

 

I created library correctly.