[Source Code] JAVA I/o------read the. zip file and the. xml file in the Zip

Source: Internet
Author: User
Tags stringbuffer

Package com.mycompany.iotest;

Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.PrintWriter;
Import java.util.Enumeration;
Import Java.util.zip.ZipEntry;
Import java.util.zip.ZipException;
Import Java.util.zip.ZipFile;

Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import javax.xml.parsers.ParserConfigurationException;

Import org.w3c.dom.Document;
Import Org.w3c.dom.Node;
Import Org.xml.sax.InputSource;
Import org.xml.sax.SAXException;

public class Filetest {


public void Readzipfile (File thefile) {
StringBuffer xmlList = new StringBuffer ();
StringBuffer directories = new StringBuffer ();
try {
ZipFile zfile = new ZipFile (thefile);
Enumeration EMU = Zfile.entries ();
int i = 1;
Object obj = null;
while (Emu.hasmoreelements ()) {
obj = Emu.nextelement ();
String temp = obj.tostring ();
System.out.println (i++ + ":" + temp);

ZipEntry entry = (zipentry) obj;
System.out.println ("Entry name =" +entry.getname ());

Collect XML files
if (Temp.endswith ("xml")) {
Xmllist.append (Temp.substring (Temp.lastindexof ("/") +1));
Xmllist.append ("n");
}

Collect directories
if (Entry.isdirectory ()) {
Directories.append (Entry.getname ());
Directories.append ("n");
}
}

System.out.println ("***********************************");
System.out.println ("XML Files:/n" +xmllist.tostring ());
SYSTEM.OUT.PRINTLN ("Directories: n" +directories);
catch (Zipexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
}

public void Readxmlinzip (File thefile) {
InputStream inputstream = null;
try {
ZipFile zip = new ZipFile (thefile);
ZipEntry entry = zip.getentry ("Web-inf/web.xml");
InputStream = Zip.getinputstream (entry);
catch (Zipexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}

BufferedReader BufferedReader = new BufferedReader (new InputStreamReader (InputStream));
InputSource InputSource = new InputSource (BufferedReader);
Documentbuilder builder = null;
Document doc = null;

Documentbuilderfactory dom = Documentbuilderfactory.newinstance ();
try {
Builder = Dom.newdocumentbuilder ();
catch (Parserconfigurationexception e) {
E.printstacktrace ();
}

try {
doc = Builder.parse (InputSource);
doc = Builder.parse (InputStream);
catch (Saxexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}

if (doc!=null) {
Node node = Doc.getfirstchild ();
System.out.println ("NamespaceURI is:" +node.getnamespaceuri ());
System.out.println ("The" "The" "The" "+node.getnodename");
} else {
System.out.println ("DOC is NULL!!!");
}
}

public static void Main (string[] args) {
Filetest ft = new Filetest ();
File Thefile = new file ("F://onlinehome.war");
File Thefile = new file ("F://struts-example.war");
Ft.readxmlinzip (thefile);
}
}

With dom4j *******************************************
Package com.mycompany.iotest;

Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.PrintWriter;
Import java.util.Enumeration;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.zip.ZipEntry;
Import java.util.zip.ZipException;
Import Java.util.zip.ZipFile;

Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import javax.xml.parsers.ParserConfigurationException;

Import org.dom4j.Document;
Import org.dom4j.DocumentException;
Import Org.dom4j.DocumentFactory;
Import org.dom4j.Element;
Import Org.dom4j.io.SAXReader;
Import Org.xml.sax.InputSource;
Import org.xml.sax.SAXException;

public class Filetest {

public void readxmlinzipbydom4j (File thefile) {
InputStream inputstream = null;
try {
ZipFile zip = new ZipFile (thefile);
ZipEntry entry = zip.getentry ("Web-inf/web.xml");
InputStream = Zip.getinputstream (entry);
catch (Zipexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}


Document domdoc = null;

Documentfactory docfactory = Documentfactory.getinstance ();

Saxreader Saxreader = new Saxreader ();
Saxreader Saxreader = new Saxreader (docfactory);

try {
Domdoc = Saxreader.read (InputStream);
catch (Documentexception e) {
E.printstacktrace ();
}



if (domdoc!=null) {
int elenum = 1;
Element element = Domdoc.getrootelement ();
System.out.println ("RootElement:" +element.getname ());
List List = Element.elements ();
for (Iterator it = List.iterator (); It.hasnext ();) {
Element elemt = (Element) It.next ();
System.out.println (elenum++ + ":" + elemt.getname ());
}
} else {
System.out.println ("Domdoc is NULL!!!");
}
}

public static void Main (string[] args) {
Filetest ft = new Filetest ();
File Thefile = new file ("F://onlinehome.war");
File Thefile = new file ("D://ut_web.war");
FT.READXMLINZIPBYDOM4J (thefile);
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.