[英]How to zip/upzip a folder and all of its files and subdirectories using Java?
I was reading this to learn how to zip/unzip files using Java. 我正在阅读本文,以了解如何使用Java压缩/解压缩文件。 I used this to guide me and it worked great when zipping all the files inside a folder, but when I tested it with a folder containing more folders inside of it, it didn't work, it threw the following error:
我用它来指导我,将所有文件压缩到一个文件夹中时效果很好,但是当我使用其中包含更多文件夹的文件夹对其进行测试时,它不起作用,它引发了以下错误:
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)
This is the class I'm using, as you will see it's the same Code Sample 4: Zip.java class code from the previous link: 这是我正在使用的类,您将看到它是相同的代码示例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();
}
}
}
What changes should be made so this code can zip folders inside folder and all of its files into a zip file? 应该进行哪些更改,以便此代码可以将文件夹内的文件夹及其所有文件压缩为zip文件?
The zip entry needs to specify the path of the file inside the archive. zip条目需要指定档案内部文件的路径。 You can't add a folder to a zip archive - you can only add the files within the folder.
您不能将文件夹添加到zip存档中-您只能在该文件夹中添加文件。
The naming convention is to use forward slashes as the path separator. 命名约定是使用正斜杠作为路径分隔符。 If you are zipping a folder with the following files/subdirectories:
如果要使用以下文件/子目录压缩文件夹:
c:\foo\bar\a.txt
c:\foo\bar\sub1\b.txt
c:\foo\bar\sub2\c.txt
the zip entry names would be: zip条目名称为:
a.txt
sub1/b.txt
sub2/c.txt
So to fix your algorithm, add isDirectory() inside your for loop, and then recursively add the files in any subdirectory to the zip. 因此,要修复算法,请在for循环内添加isDirectory(),然后以递归方式将任何子目录中的文件添加到zip中。 Probably the best way to do this is to have a method:
可能最好的方法是有一个方法:
addDirectoryToZip(String prefix, File directory, ZipOutputStream out)
Here's a solution for the problem: java.util.zip - Recreating directory structure 这是该问题的解决方案: java.util.zip-重新创建目录结构
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.