import java.io.*;
import java.util.*;
import java.util.zip.*;public class ZipCompress {
// Throw exceptions to console:
public static void main(String[] args) throws IOException {
args = new String[]{"res/zip1.txt","res/zip2.txt","res/zip3.txt"};
FileOutputStream f = new FileOutputStream("res/test.zip");
CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
ZipOutputStream zos = new ZipOutputStream(csum);
BufferedOutputStream out = new BufferedOutputStream(zos);
zos.setComment("A test of Java Zipping");
// No corresponding getComment(), though.
for (int i = 0; i < args.length; i++) {
System.out.println("Writing file " + args[i]);
BufferedReader in = new BufferedReader(new FileReader(args[i]));
zos.putNextEntry(new ZipEntry(args[i]));
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
in.close();
}
out.close();
// Checksum valid only after the file has been closed!
System.out.println("Checksum: " + csum.getChecksum().getValue());
// Now extract the files:
System.out.println("Reading file");
FileInputStream fi = new FileInputStream("res/test.zip");
CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());
ZipInputStream in2 = new ZipInputStream(csumi);
BufferedInputStream bis = new BufferedInputStream(in2);
ZipEntry ze;
while ((ze = in2.getNextEntry()) != null) {
System.out.println("Reading file " + ze);
int x;
while ((x = bis.read()) != -1) {
System.out.write(x);
}
}
System.out.println("Checksum: " + csumi.getChecksum().getValue());
// Alternative way to open and read zip files:
ZipFile zf = new ZipFile("res/test.zip");
Enumeration e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze2 = (ZipEntry) e.nextElement();
System.out.println("File: " + ze2);
// ... and extract the data as before
}
}}
zip1.txt:
1
zip1.txt:
2
zip1.txt:
3这段程序会把"res/zip1.txt","res/zip2.txt","res/zip3.txt"这3个文件压缩成test.zip
test.zip中也有这3个文件,但是其中的内容却和压缩前有些出入,它们会变成
zip1.txt:zip1.txt:zip1.txt:
1
2
3请问,有什么问题呢,我看程序好像也没有什么不对的啊?
请教了!
import java.util.*;
import java.util.zip.*;public class ZipCompress {
// Throw exceptions to console:
public static void main(String[] args) throws IOException {
args = new String[]{"res/zip1.txt","res/zip2.txt","res/zip3.txt"};
FileOutputStream f = new FileOutputStream("res/test.zip");
CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
ZipOutputStream zos = new ZipOutputStream(csum);
BufferedOutputStream out = new BufferedOutputStream(zos);
zos.setComment("A test of Java Zipping");
// No corresponding getComment(), though.
for (int i = 0; i < args.length; i++) {
System.out.println("Writing file " + args[i]);
BufferedReader in = new BufferedReader(new FileReader(args[i]));
zos.putNextEntry(new ZipEntry(args[i]));
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
in.close();
}
out.close();
// Checksum valid only after the file has been closed!
System.out.println("Checksum: " + csum.getChecksum().getValue());
// Now extract the files:
System.out.println("Reading file");
FileInputStream fi = new FileInputStream("res/test.zip");
CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());
ZipInputStream in2 = new ZipInputStream(csumi);
BufferedInputStream bis = new BufferedInputStream(in2);
ZipEntry ze;
while ((ze = in2.getNextEntry()) != null) {
System.out.println("Reading file " + ze);
int x;
while ((x = bis.read()) != -1) {
System.out.write(x);
}
}
System.out.println("Checksum: " + csumi.getChecksum().getValue());
// Alternative way to open and read zip files:
ZipFile zf = new ZipFile("res/test.zip");
Enumeration e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze2 = (ZipEntry) e.nextElement();
System.out.println("File: " + ze2);
// ... and extract the data as before
}
}}
zip1.txt:
1
zip1.txt:
2
zip1.txt:
3这段程序会把"res/zip1.txt","res/zip2.txt","res/zip3.txt"这3个文件压缩成test.zip
test.zip中也有这3个文件,但是其中的内容却和压缩前有些出入,它们会变成
zip1.txt:zip1.txt:zip1.txt:
1
2
3请问,有什么问题呢,我看程序好像也没有什么不对的啊?
请教了!
楼主【our651】截止到2008-07-08 16:26:54的历史汇总数据(不包括此帖):
发帖的总数量:7 发帖的总分数:120 每贴平均分数:17
回帖的总数量:67 得分贴总数量:25 回帖的得分率:37%
结贴的总数量:7 结贴的总分数:120
无满意结贴数:1 无满意结贴分:20
未结的帖子数:0 未结的总分数:0
结贴的百分比:100.00% 结分的百分比:100.00%
无满意结贴率:14.29 % 无满意结分率:16.67 %
敬礼!
1
zip1.txt:
2
zip1.txt:
3
是不是因为文件名是相同的原因,PS
同一文件夹下可以有同名的文件吗如果不能的话是不是你输入的参数错误了
zos.putNextEntry(new ZipEntry(args[i]));
这个是根据你输入的参数来定制压缩的
不该意思输入错误
是
zip1.txt:
1
zip2.txt:
2
zip3.txt:
3