我正在阅读这篇文章以了解如何使用 Java 压缩/解压缩文件。我用它来指导我,它在将所有文件压缩到一个文件夹中时效果很好,但是当我用一个包含更多文件夹的文件夹对其进行测试时,它不起作用,它抛出了以下错误:
java.io.FileNotFoundException: assets (Access is denied) //assets is the name of the folder I tried to zip
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at Zip.main(Zip.java:24)
这是我正在使用的类,您将看到它与上一个链接中的代码示例 4:Zip.java类代码相同:
import java.io.*;
import java.util.zip.*;
public class Zip {
static final int BUFFER = 2048;
public void zip() {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new
FileOutputStream("H:\\myfigs.zip");
CheckedOutputStream checksum = new
CheckedOutputStream(dest, new Adler32());
ZipOutputStream out = new
ZipOutputStream(new
BufferedOutputStream(checksum));
//out.setMethod(ZipOutputStream.DEFLATED);
byte data[] = new byte[BUFFER];
// get a list of files from current directory
File f = new File(".");
String files[] = f.list();
for (int i=0; i<files.length; i++) {
System.out.println("Adding: "+files[i]);
FileInputStream fi = new
FileInputStream(files[i]);
origin = new
BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(files[i]);
out.putNextEntry(entry);
int count;
while((count = origin.read(data, 0,
BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
System.out.println("checksum: "+checksum.getChecksum().getValue());
} catch(Exception e) {
e.printStackTrace();
}
}
}
应该进行哪些更改,以便此代码可以将文件夹内的文件夹及其所有文件压缩到一个 zip 文件中?