我试图将文件从zip文件中的文件夹中放入以下结构:
Folder structure: myFolder |-file1.txt |-file2.txt |-folder172 |-file817.txt |-file818.txt ... Supposed structure inside ZipFile: file1.txt file2.txt folder172 |-file817.txt |-file818.txt
这是我的代码:
public static void writeZip(String path) throws IOException{ FileOutputStream fos = new FileOutputStream(path+File.separator+"atest.zip"); ZipOutputStream zos = new ZipOutputStream(fos); try { Files.walk(Paths.get(path)).filter(Files::isRegularFile).forEach((string) -> addToZipFile(string.toString(),zos)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } zos.close(); fos.close(); } public static void addToZipFile(String fileName, ZipOutputStream zos) throws IOException { System.out.println("Writing '" + fileName + "' to zip file"); File file = new File(fileName); FileInputStream fis = null; 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(); }
现在的问题是,当我打电话时writeZip("/home/arthur/.grutil/");
,我在zip文件中得到以下结构:
home |-arthur |-.grutil |-file1.txt |-file2.txt |-folder172 |-file817.txt |-file818.txt ...
我如何更改我的代码以获得所谓的结构(如上所述)而不是具有完整路径'/home/arthur/.grutil/ ...'的结构?