How to add a file in a specific folder (in the ZIP)

我的未来我决定 提交于 2019-12-20 04:24:57

问题


The following code, how to add a file to a zip using java:

String source = "C:/Users/XXXXX/Desktop/Helicopter.zip";
try {
  ZipFile zipFile = new ZipFile(source);

  ZipParameters parameters = new ZipParameters();

  zipFile.addFile(new File("C:/Users/XXXXXX/Desktop/HELLO_HELICOPTER.txt"), parameters);


} catch (net.lingala.zip4j.exception.ZipException e) {
  e.printStackTrace();
}

How am I able to add the file in a specific folder in this zip-archive?


回答1:


I found the solution:

Put the file you want to add in a specific folder and then use the method "addFolder()".

All files within this folder will be added and if the same named folder is already in the zip, your folder will not be re-created, but the files will be added to the existing one

Example:

ZipFile zipFile = new ZipFile(source);
ZipParameters parameters = new ZipParameters();

zipFile.addFolder(new File("C:/Users/XXXXXX/Desktop/HELLO_Folder), parameters);


来源:https://stackoverflow.com/questions/22461428/how-to-add-a-file-in-a-specific-folder-in-the-zip

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!