我在创建zip文件时遇到问题,该文件包含项目结构之外的文件(例如:C:/ folder / subfolder)。会发生什么是zip文件将包含放置文件的整个文件夹结构。 这是代码:
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("f:/arhiva.zip");
ZipOutputStream zos = new ZipOutputStream(fos);
String file1Name = "c:/zer/HOTTT.pdf";
String file2Name = "c:/zer/fisx.docx";
String file3Name = "c:/zer/fisx.xlsx";
addToZipFile(file1Name, zos);
addToZipFile(file2Name, zos);
addToZipFile(file3Name, zos);
zos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
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)
我已经解决了这个问题。似乎使用Path解决了这个问题。
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("f:/arhivaB.zip");
ZipOutputStream zos = new ZipOutputStream(fos);
Path calea = Paths.get("c:\\bin\\HOTTT.pdf");
File fisa = calea.toFile();
Path caleb = Paths.get("c:\\bin\\fisx.docx");
File fisb = caleb.toFile();
Path calec = Paths.get("c:\\bin\\fisx.xlsx");
File fisc = calec.toFile();
addToZipFile(fisa, zos);
addToZipFile(fisb, zos);
addToZipFile(fisc, zos);
// addToZipFile(file1Name, zos);
// addToZipFile(file2Name, zos);
// addToZipFile(file3Name, zos);
zos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void addToZipFile(/*String fileName*/ File fisa, ZipOutputStream zos) throws FileNotFoundException, IOException {
// System.out.println("Writing '" + fileName + "' to zip file");
// File file = new File(fileName);
FileInputStream fis = new FileInputStream(fisa);
ZipEntry zipEntry = new ZipEntry(fisa.getName());
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();
}