六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 29|回复: 0

java操作压缩文件

[复制链接]

升级  30%

3

主题

3

主题

3

主题

童生

Rank: 1

积分
15
 楼主| 发表于 2013-1-26 13:35:44 | 显示全部楼层 |阅读模式
java io中提供了丰富的文件操作工具,其中对压缩文件的支持也很方便,下面说明对zip文件做多文件读写的用法:

public class file2 {
 
public static void main(String[] args) throws Exception {
FileOutputStream f = new FileOutputStream("test.zip");
CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
ZipOutputStream zos = new ZipOutputStream(csum);
BufferedOutputStream out = new BufferedOutputStream(zos);
zos.setComment("A testing of java zip");
String[] s = { "1.txt", "2.txt" };
for (String ss : s) {
System.out.println("wrirting file:" + ss);
BufferedReader in = new BufferedReader(new FileReader(ss));
zos.putNextEntry(new ZipEntry(ss));
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
in.close();
out.flush();
}
out.close();
System.out.println(csum.getChecksum().getValue());
 
System.out.println("reading……");
FileInputStream f1 = new FileInputStream("test.zip");
CheckedInputStream csum1 = new CheckedInputStream(f1, new Adler32());
ZipInputStream in2 = new ZipInputStream(csum1);
BufferedInputStream bis = new BufferedInputStream(in2);
ZipEntry ze;
while ((ze = in2.getNextEntry()) != null) {
System.out.println(ze);
int x;
while ((x = bis.read()) != -1) {
System.out.write(x);
}
System.out.println();
}
System.out.println(csum1.getChecksum().getValue());
bis.close();
ZipFile zf = new ZipFile("test.zip");
Enumeration e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze2 = (ZipEntry) e.nextElement();
System.out.println(ze2);
}
 
}
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表