java zip xml_java 读取zip里面的xml文件

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 classReadXMLFromZIP {public static voidmain(String[] args) throws Exception {

String zipfile_dir= "D:\\test.zip";try{

readZipFile(zipfile_dir);

}catch(Exception e) {

e.printStackTrace();

}

}public static voidreadZipFile(String file) throws Exception {

readZipFile(newFile(file));

}public static voidreadZipFile(File file) throws Exception {

ZipFile zf= new ZipFile(file, Charset.forName("utf8"));

InputStreamin = new BufferedInputStream(newFileInputStream(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), "utf8"));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= newFile(ze.getName());

}

FileOutputStreamout = newFileOutputStream(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();

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以按照以下步骤在Java中实现通过Web服务接口传输压缩XMLZIP文件: 1. 生成要传输的XML文件并将其压缩为ZIP文件。 2. 创建一个Web服务接口,将ZIP文件作为输入参数传递到该接口。 3. 在Web服务接口的实现中,使用Java的压缩文件API将ZIP文件解压缩,并读取XML内容。 4. 对XML内容执行所需的操作,并将结果作为输出返回到Web服务接口调用方。 以下是一个基本的示例代码,用于在Java中实现通过Web服务接口传输压缩XMLZIP文件: ``` @WebService public class MyWebService { @WebMethod public String processZipFile(byte[] zipFile) { // 解压缩ZIP文件 try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(zipFile); ZipInputStream zipInputStream = new ZipInputStream(byteArrayInputStream)) { ZipEntry entry = zipInputStream.getNextEntry(); if (entry != null) { // 读取XML内容 byte[] xmlBytes = new byte[(int) entry.getSize()]; zipInputStream.read(xmlBytes); // 对XML内容执行所需的操作 String result = processXml(xmlBytes); return result; } } catch (IOException e) { e.printStackTrace(); } return ""; } private String processXml(byte[] xmlBytes) { // 对XML内容执行所需的操作 return "XML processing result"; } } ``` 在此示例中,`processZipFile`方法接收一个`byte[]`类型的ZIP文件,使用Java的压缩文件API解压缩ZIP文件,并读取XML内容。然后对XML内容执行所需的操作,并将结果作为输出返回到Web服务接口调用方。 请注意,此示例仅用于演示目的,您需要根据自己的具体需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值