我正在使用以下代码创建一个Zip文件。我想在方法createZipFromFiles中将创建的Zip作为File对象返回,以便在其他地方使用。可以通过FileOutputStream或ZipOutputStream完成吗?
public File createZipFromFiles(String zipName, List<File> files) {
try {
FileOutputStream fos = new FileOutputStream(zipName);
ZipOutputStream zos = new ZipOutputStream(fos);
for (File file : files) {
addToZipFile(file.getName(), zos);
}
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void addToZipFile(String fileName, ZipOutputStream zos) throws FileNotFoundException, IOException {
System.out.println("Writing '" + fileName + "' to zip file");
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(fileName);
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
fis.close();
}
答案 0 :(得分:0)
刚回来
new File(zipName);
因为您已经有了zip文件名