zipOutputStream.putNextEntry(new ZipEntry(fileName))
,
这里fileName是将在zip
中显示的名称所以我这样解决的是:
zos.putNextEntry(new ZipEntry(file3.replace("C:/1/hisenyuan","")));
I want zip folder:C:\1\hisenyuan
主要代码:
String file3 = file2.getAbsolutePath();
try {
is = new FileInputStream(file3);
//here the file3 is the name that will show inside zip
zos.putNextEntry(new ZipEntry(file3.replace("C:/1/hisenyuan","")));
int temp = 0;
int bufferSize = 1024 * 5;
byte[] buffer = new byte[bufferSize];
while ((temp = is.read(buffer, 0, bufferSize)) != -1) {
zos.write(buffer, 0, temp);
zos.flush();
}
以我的方式拉链的文件夹结构是:
C:\1\hisenyuan\build.png
C:\1\hisenyuan\DSCN6812.JPG
C:\1\hisenyuan\test\test\hello.zip
C:\1\hisenyuan\test\hisenyuan.zip
C:\1\hisenyuan\test\test.txt
C:\1\hisenyuan\test\test\hello\hello.txt
C:\1\hisenyuan\tomcat.png
我希望zip的文件夹结构是:
build.png
DSCN6812.JPG
test\hello.zip
test\hisenyuan.zip
test\test.txt
test\hello\hello.txt
tomcat.png