说明:
1、文件内容采用XXXReader、XXXWriter面向字符的处理类
2、由于jdk1.6的ZipOutputStream没有方法setEncoding(encoding),故采用ant.jar的ZipOutputStream.
ant包,可以在官方网站http://ant.apache.org/bindownload.cgi下载,把ant.jar导入到类中,
我下载的是apache-ant-1.8.2-bin.zip,解压后得到相关jar包 和 关联文档(读文档很必要的哦)
具体见代码:
代码不规范、有点乱,请见谅
package io;
//: io/ZipCompress2.java
// Uses Zip compression to compress any
// number of files given on the command line.
// {Args: ZipCompress2.java}
//import java.util.zip.*;
import org.apache.tools.zip.*; //不使用jdk的rt.jar中的类,转而使用ant.jar中的类。 Apache Ant is a Java library and command-line tool that help building software
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.Adler32;
import java.util.zip.ZipInputStream;
import java.io.*;
import java.util.*;
import static net.mindview.util.Print.*;
//ant包,可以在官方网站http://ant.apache.org/bindownload.cgi下载,把ant.jar导入到类中
public class ZipCompress2 {
public static void main(String[] args)
throws IOException {
//(1)使用org.apache.tools.zip.ZipOutputStream 写压缩包test测试_ant.zip到文件系统
FileOutputStream f = new FileOutputStream("test测试_ant.zip");
CheckedOutputStream csum =
new CheckedOutputStream(f, new Adler32());
ZipOutputStream zos = new ZipOutputStream(csum); //
String encoding=System.getProperty("file.encoding");
zos.setEncoding(encoding);
//BufferedOutputStream out =
//new BufferedOutputStream(zos);
PrintWriter out = new PrintWriter(zos);
zos.setComment("A test of Java Zipping中文注释"); //???怎么解决中文注解显示为乱码的情况????? 采用ant.jar中的压缩相关类
// No corresponding getComment(), though.
for(String arg : args) {
print("Writing file " + arg);
BufferedReader in =new BufferedReader(new FileReader(arg));
zos.putNextEntry(new ZipEntry(arg));//加入归档包/压缩包的每一个文件,都对应一个ZipEntry。 ???怎么解决文件名为中文显示为乱码的情况????? 采用ant.jar中的压缩相关类
/* 1.For each file to add to the archive, you must call putNextEntry( ) and pass it a ZipEntry object.
*
* The ZipEntry object contains an extensive interface that allows you to get and set all the data
* available on that particular entry in your Zip file: name, compressed and uncompressed sizes,
*date, CRC checksum, extra field data, comment, compression method, and whether it’s a directory entry */
int c;
while((c = in.read()) != -1)
out.write(c);//2.BufferedOutputStream out = new BufferedOutputStream(zos);
/*通过out将数据输出/写入到zos,但是每一个文件对应一个in=new BufferedReader(new FileReader(arg)),
*每一个文件对应一个ZipEntry:zos.putNextEntry(new ZipEntry(arg))*/
in.close();
out.flush();
}
out.close();
//(2)
/*由于在java.util.zip.ZipOutputStream中对ZipEntry.name采用的是utf-8编码: byte[] nameBytes = getUTF8Bytes(e.name)
* java.util.zip.ZipInputStream中也是对ZipEntry.name采用的是utf-8编码:ZipEntry e = createZipEntry(getUTF8String(b, 0, len))
所以在读取zip包中的文件时,可以兼容(都是用utf-8编码文件名)
但是在ant.jar的org.apache.tools.zip.* 中没有ZipInputStream类,这是就只能使用org.apache.tools.zip.ZipFile了.
当使用java.util.zip.ZipInputStream时报错:
at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:303)
这是因为org.apache.tools.zip.ZipOutputStream使用GB18030编码文件名,却在java.util.zip.ZipInputStream中使用utf-8解码文件名
*/
/*
// Checksum valid only after the file has been closed!
print("Checksum: " + csum.getChecksum().getValue()); //In order to read the checksum, you must somehow have access to the associated Checksum(表示数据校验和的接口) object
// Now extract the files:
print("Reading file");
FileInputStream fi = new FileInputStream("test测试_ant.zip");
CheckedInputStream csumi =
new CheckedInputStream(fi, new Adler32());
ZipInputStream zis = new ZipInputStream(csumi);
//BufferedInputStream bis = new BufferedInputStream(zis);
BufferedReader bis=new BufferedReader( new InputStreamReader(zis));
java.util.zip.ZipEntry ze;
while((ze = zis.getNextEntry()) != null) {
print("Reading file " + ze);
int x;
while((x = bis.read()) != -1)
System.out.print((char)x); //System.out.write()是面向字节的
print();
}
if(args.length == 1)
print("Checksum: " + csumi.getChecksum().getValue());
bis.close();
print();
*/
//(3)采用org.apache.tools.zip.ZipFile读取zip文件
org.apache.tools.zip.ZipFile zf=new org.apache.tools.zip.ZipFile("test测试_ant.zip",encoding);
Enumeration<org.apache.tools.zip.ZipEntry> e_zEntries=zf.getEntries();
while(e_zEntries.hasMoreElements()){
org.apache.tools.zip.ZipEntry ze=e_zEntries.nextElement();
print("File: " + ze);
BufferedReader bf=new BufferedReader( new InputStreamReader(zf.getInputStream(ze)) );
int x;
while((x = bf.read()) != -1)
System.out.print((char)x); //System.out.write()是面向字节的
print();
}
}
} /* (Execute to see output) *///:~
分享到:
相关推荐
java_util_concurrent_user_guide.zipjava_util_concurrent_user_guide.zipjava_util_concurrent_user_guide.zipjava_util_concurrent_user_guide.zipjava_util_concurrent_user_guide.zipjava_util_concurrent_user...
thinking_in_java-master.zip
IBM WAS8.5配置WS_SDK_JAVA_TECH_7.0.6.1__TRIAL,里面包含WS_SDK_JAVA_TECH_7.0.6.1__TRIAL资源和配置步骤。
java.io.File zipfile =new java.io.File(zipf); java.io.File[] srcfile=new java.io.File[20]; //自己把要压缩的文件加进去 int m=0; //---------压缩包中文件的个数-------------- byte[] buf=new byte[1024];...
java______反编译工具.zip
18.java_helloworld案例编写.zip18.java_helloworld案例编写.zip18.java_helloworld案例编写.zip18.java_helloworld案例编写.zip18.java_helloworld案例编写.zip18.java_helloworld案例编写.zip18.java_helloworld...
人脸识别Windows开发包 64位 解压即用,包括Demo文件
下载的最新版Alibaba_Java_Coding_Guidelines-2.0.2.zip,解压后里面有个zip离线包,通过ide离线安装,测试真实可用。 适合IDE2018.1.6版本。
基于Java开源内容管理系统项目源码.zip基于Java开源内容管理系统项目源码.zip基于Java开源内容管理系统项目源码.zip基于Java开源内容管理系统项目源码.zip基于Java开源内容管理系统项目源码.zip基于Java开源内容管理...
Thinking in Java 4th Edition 习题答案,配合我上传的 Thinking in Java 4th Edition.pdf 使用。
包含翻译后的API文档:scala-java8-compat_2.11-0.7.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.scala-lang.modules:scala-java8-compat_2.11:0.7.0; 标签:scala、lang、modules、java8、compat、中文...
下载的最新版Alibaba_Java_Coding_Guidelines-2.1.1.zip,解压后里面有个zip离线包,通过ide离线安装,测试真实可用。
Java桌面_坦克大战小游戏.zipJava桌面_坦克大战小游戏.zip Java桌面_坦克大战小游戏.zipJava桌面_坦克大战小游戏.zip Java桌面_坦克大战小游戏.zipJava桌面_坦克大战小游戏.zip Java桌面_坦克大战小游戏.zipJava桌面...
19.java基础概念_注释.zip19.java基础概念_注释.zip19.java基础概念_注释.zip19.java基础概念_注释.zip19.java基础概念_注释.zip19.java基础概念_注释.zip19.java基础概念_注释.zip19.java基础概念_注释.zip19.java...
基于java的开发源码-异步IO框架 Cindy.zip 基于java的开发源码-异步IO框架 Cindy.zip 基于java的开发源码-异步IO框架 Cindy.zip 基于java的开发源码-异步IO框架 Cindy.zip 基于java的开发源码-异步IO框架 Cindy.zip ...
20.java基础概念_关键词.zip20.java基础概念_关键词.zip20.java基础概念_关键词.zip20.java基础概念_关键词.zip20.java基础概念_关键词.zip20.java基础概念_关键词.zip20.java基础概念_关键词.zip20.java基础概念_...
linux centos java opencv4.5.1开发依赖,由于在本地是windows环境,直接在opencv官网下载windows版本下载后,即可得到opencv-451.jar和opencv_java451.dll,可以愉快的开发及测试,但是测通过之后需要部署到服务器...
本资源含有基于java的memcached 的数据缓存开发所需的 memcached-1.2.1-win32.zip 和 java_memcached-release_1.6.zip 快来一起学习吧
Java Platform, Enterprise Edition 8 SDK Update 1 Java EE 8 continues to improve API and programming models needed for today's applications and adds features requested by our world-wide community. ...
Java聊天软件chat_src用定制的GUI开发