Java 使用整个文件夹结构的内容创建 zip 文件
Java creating zip file with the contents of the entire folder structure
我在创建 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();
}
请帮帮我!
我已经解决了问题。看来使用 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();
}
我在创建 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();
}
请帮帮我!
我已经解决了问题。看来使用 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();
}