Java读取zip压缩包下xml文件

刚好公司项目有这样一个需求,就把成果拿出来记录一下!

 

功能需求: Java读取zip压缩包下xml文件,并显示xml文件中标签中的内容。

 

代码实现:

package com.huangzijing.test;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class ReadXMLFromZIP {
	
	public static void main(String[] args) throws Exception {

		String zipfile_dir = "D:\\test.zip";
		try {
			readZipFile(zipfile_dir);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void readZipFile(String file) throws Exception {
		
		readZipFile(new File(file));
	}

	public static void readZipFile(File file) throws Exception {

		ZipFile zf = new ZipFile(file, Charset.forName("GBK"));
		InputStream in = new BufferedInputStream(new FileInputStream(file));
		ZipInputStream zis = new ZipInputStream(in);
		ZipEntry ze;
		while ((ze = zis.getNextEntry()) != null) {
			if (ze.isDirectory()) {
			} else {
				if (ze.getName().endsWith(".xml")) {
					System.err.println("file - " + ze.getName() + " : " + ze.getSize() + " bytes");
					if (ze.getSize() > 0) {
						File new_xml_file = null;
						BufferedReader br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze), "GBK"));
						if ((ze.getName().trim().lastIndexOf("/")) != -1) {
							new_xml_file = new File(ze.getName().substring(ze.getName().trim().lastIndexOf("/")+1));
							System.out.println(new_xml_file);
						} else {
							new_xml_file = new File(ze.getName());
						}
						FileOutputStream out = new FileOutputStream(new_xml_file);
						BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
						String line;
						while ((line = br.readLine()) != null) {
//							 System.out.println(line);
							bw.write(line);
						}
						br.close();
						bw.close();
						
						DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
						DocumentBuilder builder = factory.newDocumentBuilder();
						Document document = builder.parse(new_xml_file);
						Element root = document.getDocumentElement();
						NodeList nodeList = root.getElementsByTagName("description");
						for (int i = 0; i < nodeList.getLength(); i++) {
							Element element = (Element) nodeList.item(i);
							System.out.println("description:" + element.getTextContent());
						}
						new_xml_file.delete();
					}
				}
			}
		}
		zis.closeEntry();
		zis.close();
		zf.close();
	}
	
}


D盘下存在test.zip文件,结构如下:

| - test.zip

| - | - test

| - | - | - test.txt

| - | - | - site.xml

| - | - | - index.html

| - | - | - chinese.xml

site.xml文件如下:



   
      Eclipse Install/Update site for Subclipse
   
   
   
      
   

   
      
         Subversion 1.7 plug-in for Eclipse 3.2 and higher.
      
   
   
      
         SVNKit Library support for Subversion 1.7.x API
      
   


chinese.xml文件如下:



   
      中文编码测试
   


运行结果:
 

file - test/chinese.xml : 161 bytes



description:      中文编码测试  



file - test/site.xml : 2298 bytes
description:      Eclipse Install/Update site for Subclipse   
description:      Subversion 1.7 plug-in for Eclipse 3.2 and higher.      
description:      SVNKit Library support for Subversion 1.7.x API   


 

 

 

你可能感兴趣的:(Java)