1.重建zip包
新建一个自己的zip包(也就是自己建立一个package),比如com.agile.zip,在这个包中把要用到的类从jdk的源码里放到这里,用eclipse可以很同快地完成这 个工作。需要所类 有:DeflaterOutputStream,InflaterInputStream,ZipConstants,ZipEntry,ZipInputStream,ZipOutputStream
上面这些类在放到com.aigle.zip包中后,在Eclipse中会显示出一些错误,这里要做得就是更改import以及其它一些工作,惟一的目的就是通过编译,不再出现编译错误。
2.修改ZipInputStream
ZipInputStream这个类中,需要修改getUTF8String这个方法,经过试验,用winRar压缩后的zip文件,其中的中文文件名是以gbk编码保存的,因此只需要在这个方法的前面加上
try {
String s = newString(b,off,len,"gbk");
return s;
} catch (UnsupportedEncodingExceptione) {
e.printStackTrace();
}
修改后的方法类似下面的代码:
private static String getUTF8String(byte[] b, intoff, int len) {
try {
String s = newString(b,off,len,"gbk");
return s;
} catch (UnsupportedEncodingExceptione) {
e.printStackTrace();
}
// First, count the number ofcharacters in the sequence
int count = 0;
int max = off + len;
...
...
}
3.去掉一些本地调用方法
原来的java.util.zip.ZipEntry里面,要加载本地库,在这里这些代码是没有用处的,去掉就可以了,而如果不去掉,会引起链接错误,很奇怪,这几个native方法我也没有找到在哪儿使用了,sun的jdk1.4.2里留着它们做什么呢?
static {
/* loadthe zip library */
java.security.AccessController.doPrivileged(
newsun.security.action.LoadLibraryAction("zip"));
initIDs();
}
然后就大功告成了。哈哈!
详细的代码见csdn的文章
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import com.glodon.zip.ZipEntry;
- import com.glodon.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.Node;
- import org.w3c.dom.NodeList;
- /**
- * 解压ZIP文件
- *
- * @author thinkpad
- *
- */
- public class TestExtZipFiles {
- /**
- * 解压含有多个文件的ZIP包
- *
- * @param zipFileName
- * @param extPlace
- */
- private static void extZipFileList(String zipFileName, String extPlace) {
- try {
- ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
- ZipEntry entry = null;
- while ((entry = in.getNextEntry()) != null) {
- String entryName = entry.getName().toLowerCase();
- if (entryName.endsWith(".xml")) {// 找到xml文件
- int i = entryName.lastIndexOf("/");
- entryName = entryName.substring(i+1);
- FileOutputStream os = new FileOutputStream(extPlace + entryName);
- // Transfer bytes from the ZIP file to the output file
- byte[] buf = new byte[1024];
- int len;
- while ((len = in.read(buf)) > 0) {
- os.write(buf, 0, len);
- }
- os.close();
- in.closeEntry();
- resolveXMLFile(extPlace + entryName);
- File f = new File(extPlace + entryName);
- if(f.exists()) {
- f.delete();
- }
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("解压文件成功 ");
- }
- public static void resolveXMLFile(String fileName) {
- DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
- try {
- DocumentBuilder dombuilder = domfac.newDocumentBuilder();
- InputStream is = new FileInputStream(fileName);
- Document doc = dombuilder.parse(is);
- Element root = doc.getDocumentElement();
- NodeList bidders = root.getChildNodes();
- if(bidders != null){
- for(int i=0;i<bidders.getLength();i++){
- //一条数据
- Node bidder = bidders.item(i);
- if(bidder.getNodeType() == Node.ELEMENT_NODE){
- String PBRQ = bidder.getAttributes().getNamedItem("PBRQ").getNodeValue();
- String JHJGSJ = bidder.getAttributes().getNamedItem("JHJGSJ").getNodeValue();
- String JHKGSJ = bidder.getAttributes().getNamedItem("JHKGSJ").getNodeValue();
- String ZGZH = bidder.getAttributes().getNamedItem("ZGZH").getNodeValue();
- String ZLJE = bidder.getAttributes().getNamedItem("ZLJE").getNodeValue();
- String ZGJ = bidder.getAttributes().getNamedItem("ZGJ").getNodeValue();
- String XMFBQK = bidder.getAttributes().getNamedItem("XMFBQK").getNodeValue();
- String ZLBZ = bidder.getAttributes().getNamedItem("ZLBZ").getNodeValue();
- String AQWMSGF = bidder.getAttributes().getNamedItem("AQWMSGF").getNodeValue();
- String CSXMF = bidder.getAttributes().getNamedItem("CSXMF").getNodeValue();
- String XMJLID = bidder.getAttributes().getNamedItem("XMJLID").getNodeValue();
- String ABBM = bidder.getAttributes().getNamedItem("ABBM").getNodeValue();
- String TJZB = bidder.getAttributes().getNamedItem("TJZB").getNodeValue();
- String PM = bidder.getAttributes().getNamedItem("PM").getNodeValue();
- String FBHJ = bidder.getAttributes().getNamedItem("FBHJ").getNodeValue();
- String FB = bidder.getAttributes().getNamedItem("FB").getNodeValue();
- String WXB = bidder.getAttributes().getNamedItem("WXB").getNodeValue();
- String XMJL = bidder.getAttributes().getNamedItem("XMJL").getNodeValue();
- String TBBJ = bidder.getAttributes().getNamedItem("TBBJ").getNodeValue();
- String ZLCN = bidder.getAttributes().getNamedItem("ZLCN").getNodeValue();
- String GQ = bidder.getAttributes().getNamedItem("GQ").getNodeValue();
- String Signin = bidder.getAttributes().getNamedItem("Signin").getNodeValue();
- String BidderName = bidder.getAttributes().getNamedItem("BidderName").getNodeValue();
- String BidderID = bidder.getAttributes().getNamedItem("BidderID").getNodeValue();
- String JCDJH = bidder.getAttributes().getNamedItem("JCDJH").getNodeValue();
- System.out.println(PBRQ+","+JHJGSJ+","+JHKGSJ+","+ZGZH+","+ZLJE+","+ZGJ+","+XMFBQK+","+ZLBZ+","+AQWMSGF+","+CSXMF+","+XMJLID+","+ABBM+","+TJZB+","+PM+","+FBHJ+","+FB+","+WXB+","+XMJL+","+TBBJ+","+
- ZLCN+","+GQ+","+Signin+","+BidderName+","+BidderID+","+JCDJH);
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- //zip的文件位置,和解压之后的xml的位置
- extZipFileList("E:\\1010111_BHWJ.zip","E:\\1010111\\");
- }
- }