Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add multiple files and folders at once? #84

Closed
ghost opened this issue Oct 9, 2019 · 13 comments
Closed

How to add multiple files and folders at once? #84

ghost opened this issue Oct 9, 2019 · 13 comments
Assignees
Labels
new-feature New feature or request

Comments

@ghost
Copy link

ghost commented Oct 9, 2019

What should I do to compress multiple files and multiple folders into the root directory? Single compression seems to be very slow, can you compress multiple files and multiple folders at once?

like this?

ZipFile().addFolders()

Also, how to delete the folder inside the archive, the api seems to only provide the delete file.

@srikanth-lingala
Copy link
Owner

srikanth-lingala commented Oct 9, 2019

To answer the two questions:

  • How to add multiple files and folders at once?
    You can achieve this by a for loop as shown below. This is probably not the most "neat" way, but this is the only way with the current version
for (File folder : folders) {
    ZipFile.addFolder(folder); //same for files
}
  • How to delete a folder inside an archive?
    ZipFile does not have an api to delete a folder inside an archive. A workaround for this would be:

    Considering that the folder name relative to the root of the zip file is /somefolder/folder2, and you want to delete this folder, then :

String folderToDelete = "somefolder/folder2";
for (FileHeader fileHeader : zipFile.getFileHeaders()) {
  if (fileHeader.getFileName.startsWith()) {
     ZipFile.deleteFile(fileHeader);
  }
}

Both the code samples above should work but are not tested by me. I wrote the code here directly, so there might be some syntax errors. But I think you get the idea.

Both of these points are good candidates for new features. The first one might be slightly complicated from an api perspective, but I will consider adding them in one of the next releases.

@ghost
Copy link
Author

ghost commented Oct 10, 2019

To answer the two questions:

  • How to add multiple files and folders at once?
    You can achieve this by a for loop as shown below. This is probably not the most "neat" way, but this is the only way with the current version
for (File folder : folders) {
    ZipFile.addFolder(folder); //same for files
}
  • How to delete a folder inside an archive?
    ZipFile does not have an api to delete a folder inside an archive. A workaround for this would be:
    Considering that the folder name relative to the root of the zip file is /somefolder/folder2, and you want to delete this folder, then :
String folderToDelete = "somefolder/folder2";
for (FileHeader fileHeader : zipFile.getFileHeaders()) {
  if (fileHeader.getFileName.startsWith()) {
     ZipFile.deleteFile(fileHeader);
  }
}

Both the code samples above should work but are not tested by me. I wrote the code here directly, so there might be some syntax errors. But I think you get the idea.

Both of these points are good candidates for new features. The first one might be slightly complicated from an api perspective, but I will consider adding them in one of the next releases.

I added a folder with the same name in zip, it is very slow, how should I speed it up?

If there is no folder with the same name in the archive, adding the file will be quick. Adding a folder with the same name takes one minute, adding a folder without a name takes only three seconds.

@LeeYoung624
Copy link
Contributor

I tested with adding the same folder to a zip file more than once. It only takes slight more time when the folder already exists in zip file.

When adding a file to a archive which containes a file with same name, zip4j will by default override the same name file(so are the folders). That means the zip4j will firstly remove the existing file in the archive, then add the new file. That's why adding a file to a archive which containes a file with same name takes more time.
And the removing file procedure is implemented by removing file by file, which means it may take more time if the folder contains a lot of files. I think that's why you take a lot more time when adding a folder with the same folder existing in the zip.

If you do not want to override the same name file in zip, you can do it like this

ZipFile zipFile = new ZipFile("/home/zipfile.zip");
ZipParameters parameters = new ZipParameters();
parameters.setOverrideExistingFilesInZip(false);
zipFile.addFolder(new File("/folder/to/add"), parameters);

Repository owner deleted a comment Oct 11, 2019
Repository owner deleted a comment Oct 11, 2019
@srikanth-lingala
Copy link
Owner

I see multiple ideas for new features here in zip4j:

  • In ' ZipFile.addFiles()' , if the file is a directory, then add all files in that directory to the zip. This way you can achieve the requirement mentioned in this issue.

  • Delete a folder inside an archive

  • Delete multiple files inside an archive at once, which is more efficient than deleting files in a loop, as the zip file has to be recreated with each deletion.

I will include these features in one of the next release (hopefully in the next release if time permits)

@ghost
Copy link
Author

ghost commented Oct 12, 2019

I see multiple ideas for new features here in zip4j:

  • In ' ZipFile.addFiles()' , if the file is a directory, then add all files in that directory to the zip. This way you can achieve the requirement mentioned in this issue.
  • Delete a folder inside an archive
  • Delete multiple files inside an archive at once, which is more efficient than deleting files in a loop, as the zip file has to be recreated with each deletion.

I will include these features in one of the next release (hopefully in the next release if time permits)

Hey bro, this is great! I waiting for your release!

@srikanth-lingala srikanth-lingala self-assigned this Oct 13, 2019
@srikanth-lingala srikanth-lingala added the new-feature New feature or request label Oct 13, 2019
@srikanth-lingala srikanth-lingala changed the title How to compress all the contents of a folder to the root directory? How to add multiple files and folders at once? Oct 20, 2019
@markgbrown
Copy link

I may have a related request. We are trying to zip up multiple files in multiple different folder locations, retaining the folder hierarchy, and utilizing the split zip functionality to keep zip sizes under 4GB. I don't currently see a way to accomplish this. Is there a workaround?

@srikanth-lingala
Copy link
Owner

@markgbrown Your requirement is to create a zip file with:

  1. Multiple files from different folder locations
  2. Retain the folder hierarchy
  3. Split zip to keep size under 4GB

Unfortunately, a combination of all 3 is not possible at the moment. You can choose any 2 of the 3, but unfortunately, not all 3 together.

@markgbrown
Copy link

@srikanth-lingala I have implemented a solution that works for us for this scenario. It allows the files in the zip to retain the same folder hierarchy as the input files. If you are interested in the solution, I can send you the files that changed and you can decide whether it makes sense to incorporate into the library. I've never had the chance to contribute to open source before, so let me know what works best.

@srikanth-lingala
Copy link
Owner

@markgbrown Sure, please send me a patch or a PR and I will have a look into it. Just to be clear, when I said it is not possible at the moment, I meant it is not possible with the current version of zip4j. When I implement the change in this issue, depending on the complexity and feasibility, I will consider integrating your change as well.

@markgbrown
Copy link

Here's a pull request for you to look at. Feel free to use any or none of it as you feel appropriate.
#116

@yixiaco
Copy link

yixiaco commented Jan 18, 2020

我觉得添加文件或文件夹能否别拆开,在程序内部进行判断,因为当我想获取一个ProgressMonitor进度监听器的时候,并不能够同时监听两个进程的任务

@srikanth-lingala
Copy link
Owner

srikanth-lingala commented Mar 2, 2020

These two features have been added in the branch delete-multiple-files-and-folders :

  • Delete a folder inside an archive
  • Delete multiple files inside an archive at once

I will merge it to master and include it in the next release.

Working on the third feature:

  • In ' ZipFile.addFiles()' , if the file is a directory, then add all files in that directory to the zip.

@srikanth-lingala
Copy link
Owner

The last feature

  • In ' ZipFile.addFiles()' , if the file is a directory, then add all files in that directory to the zip.

is a bit complicated and tricky to implement. This will also break backward compatibility. It might be that there are users of zip4j adding files to a zip which is a directory and not expecting the whole directory to be added. If this change is now done, it might break their logic. And also there are some complicated questions of what the rootDirectory of the file (which is a folder) should be because the root directory of this file can be different than the other files being added.

The other two features:

  • Delete a folder inside an archive
  • Delete multiple files inside an archive at once

have long been implemented and released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-feature New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants