new_file_name 类似于 2013-03-15-08:59:10_65.zip
fileZip = new ZipOutputStream(new FileOutputStream(new File(new_file_name)));
byte[] buffer = new byte[1024];
try{
for(String fileName:fileList)
{
FileInputStream in = null;
try{
File file = new File(fileName);
ZipEntry ze = new ZipEntry(fileName);
fileZip.putNextEntry(ze);
in = new FileInputStream(file);
int len = 0;
while((len = in.read(buffer)) > 0) {
fileZip.write(buffer, 0, len);
}
fileZip.closeEntry();
in.close();
} catch (Exception e) {
log(0, "Exception writing "+fileName+" to "+new_file_name+": "+e.toString());
}
我收到此异常 Exception writing to 2013-03-15-09:28:20_65.zip: java.io.FileNotFoundException: (No such file or directory)
该目录具有完全权限。我也看到在文件夹中创建了一个文件,我也尝试给出 getAbsolutePath(),仍然给了我同样的异常。
最佳答案
我编写了一些实用方法,使用 NIO.2 文件 API(该库是开源的)将目录复制到 Zip 文件或从 Zip 文件复制目录:
Maven:
<dependency>
<groupId>org.softsmithy.lib</groupId>
<artifactId>softsmithy-lib-core</artifactId>
<version>0.3</version>
</dependency>
教程:
http://softsmithy.sourceforge.net/lib/current/docs/tutorial/nio-file/index.html#AddZipResourceSample
也许您发现它很有用。
关于java - ZipOutputStream 提示 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15433929/