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

addFolder issue #169

Closed
branimir-vujicic opened this issue Apr 2, 2020 · 22 comments
Closed

addFolder issue #169

branimir-vujicic opened this issue Apr 2, 2020 · 22 comments
Assignees

Comments

@branimir-vujicic
Copy link

Hi, is there an option to include empty folders in archive. I'm trying to create backup archive for my folders, but empty folders are missing in created zip file.

@srikanth-lingala
Copy link
Owner

I just tried to add a folder which contains empty folders inside it, and it had the empty folders in the generated zip file. And even the empty folder was created when extracting the same zip file again with zip4j. It could be that the tool which shows/extracts the zip file in your case, ignore empty directories. Can you attach a sample zip file generated by zip4j which was supposed to have empty folders in it?

@branimir-vujicic
Copy link
Author

Hi, I'm using ubuntu 18.04, oracle java 8 version 1.8.0_241.
The folder I'm trying to archive contains empty folders (no files, just other empty folders).
This is a list of folder :
Screenshot from 2020-04-05 21-02-50
and this is a screenshot of the zip archive:
Screenshot from 2020-04-05 21-07-36
library version is 2.5.0
here is my code:

   public Path createArchiveZip4j(Path sourceDir, Path zipDestinationDir, String sessionUid) throws InternalErrorException {

   	Path zipPath = zipDestinationDir.resolve(sessionUid + ".zip");
   	try {

   		ZipParameters parameters = new ZipParameters();
   		parameters.setCompressionMethod(CompressionMethod.DEFLATE);
   		parameters.setCompressionLevel(CompressionLevel.NORMAL);
   		parameters.setIncludeRootFolder(false);

   		net.lingala.zip4j.ZipFile zipFile = new net.lingala.zip4j.ZipFile(zipPath.toFile());

   		// Add a folder to the zip file
   		zipFile.addFolder(sourceDir.toFile(), parameters);
   	} catch (ZipException e) {
   		log.error(ERROR_CREATING_ARCHIVE);
   		throw new InternalErrorException(ERROR_CREATING_ARCHIVE, e);
   	}
   	return zipPath;
   }

@srikanth-lingala
Copy link
Owner

If I understand the issue right from the screenshot, zip4j missed to add some directories, for example "matrices", "sdfsdfsd", etc, right? Does these directories have any content inside them like other directories or files? Was the zip file already existing before your code executed?

I was still not able to reproduce this with a similar directory structure as you. My guess is that the files api is not returning the directories that in the end zip4j missed to add. Can you please set a breakpoint at AddFilesToZipTask.getFilesToAdd() line 66 as shown in the image below, and let me know if you see the directories that are missed in the zip created by zip4j? Maybe you can also post a screenshot?

Bildschirmfoto 2020-04-08 um 19 41 22

@branimir-vujicic
Copy link
Author

From the previous example, folder matrices contain only empty folders.
Zip file didn't exist before the execution of the program.

This is a list of the folder to archive :
(folders starting with folder_with_files contain files, folder starting with folder_without_files contain only other folders and no files)

temp/zip_test/to_archive/
└── test
    ├── folder_with_files_01
    │   ├── 981850.jpg
    │   └── folder_with_files_01_01
    │       └── download.png
    ├── folder_with_files_02
    │   └── Neon.jpg
    ├── folder_without_files_01
    │   └── folder_without_files_01_01
    └── folder_without_files_02

Here's complete code for example:

import lombok.extern.slf4j.Slf4j;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.CompressionLevel;
import net.lingala.zip4j.model.enums.CompressionMethod;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

@Slf4j
public class ZipTest {

	private static final String ERROR_CREATING_ARCHIVE = "Error creating archive !";

	public static void main(String[] args) {

		Path sourceDir      = Paths.get("/home/bane/temp/zip_test/to_archive");
		Path destinationDir = Paths.get("/home/bane/temp/zip_test/archive");

		String sessionUuid = UUID.randomUUID().toString();

		ZipTest test = new ZipTest();
		try {
			Path zipFile = test.createArchiveZip4j(sourceDir, destinationDir, sessionUuid);
			log.debug(zipFile.toString());
		} catch (Exception e) {
			log.error(ERROR_CREATING_ARCHIVE, e);
		}
	}

	public Path createArchiveZip4j(Path sourceDir, Path zipDestinationDir, String sessionUid) throws Exception {

		Path zipPath = zipDestinationDir.resolve(sessionUid + ".zip");
		try {

			ZipParameters parameters = new ZipParameters();
			parameters.setCompressionMethod(CompressionMethod.DEFLATE);
			parameters.setCompressionLevel(CompressionLevel.NORMAL);
			parameters.setIncludeRootFolder(false);

			net.lingala.zip4j.ZipFile zipFile = new net.lingala.zip4j.ZipFile(zipPath.toFile());

			// Add a folder to the zip file
			zipFile.addFolder(sourceDir.toFile(), parameters);
		} catch (Exception e) {
			log.error(ERROR_CREATING_ARCHIVE, e);
			throw new Exception(ERROR_CREATING_ARCHIVE);
		}
		return zipPath;
	}
}

This is the list of files from the brake point:

filesToAdd = {java.util.ArrayList@867}  size = 10
 0 = {java.io.File@876} "/home/bane/temp/zip_test/to_archive/test"
 1 = {java.io.File@877} "/home/bane/temp/zip_test/to_archive/test/folder_without_files_02"
 2 = {java.io.File@878} "/home/bane/temp/zip_test/to_archive/test/folder_without_files_01"
 3 = {java.io.File@879} "/home/bane/temp/zip_test/to_archive/test/folder_without_files_01/folder_without_files_01_01"
 4 = {java.io.File@880} "/home/bane/temp/zip_test/to_archive/test/folder_with_files_01"
 5 = {java.io.File@881} "/home/bane/temp/zip_test/to_archive/test/folder_with_files_01/981850.jpg"
 6 = {java.io.File@882} "/home/bane/temp/zip_test/to_archive/test/folder_with_files_01/folder_with_files_01_01"
 7 = {java.io.File@883} "/home/bane/temp/zip_test/to_archive/test/folder_with_files_01/folder_with_files_01_01/download.png"
 8 = {java.io.File@884} "/home/bane/temp/zip_test/to_archive/test/folder_with_files_02"
 9 = {java.io.File@885} "/home/bane/temp/zip_test/to_archive/test/folder_with_files_02/Neon.jpg"

this is archive created by OS ( nautilus -> compress)
test.zip

and this is an archive created by the example program
ec84b720-c207-49f4-9e6e-9e9c0e066381.zip

@srikanth-lingala
Copy link
Owner

The zip file generated by zip4j ec84b720-c207-49f4-9e6e-9e9c0e066381.zip has the empty directories in it. Screenshot below shows the files inside the zip file when zip4j reads your zip file:

Bildschirmfoto 2020-04-09 um 14 59 41

This is the content after zip4j extracted that zip file. As you can see there were empty directories also extracted.

Bildschirmfoto 2020-04-09 um 14 59 08

Maybe the tool which you use to extract does not extract empty directories from a zip. Or maybe if you are just viewing the zip file, the tool does not show empty directories in its view.

@branimir-vujicic
Copy link
Author

Hi,
this is what I see in the Ubuntu archive manager:
Screenshot from 2020-04-09 15-17-15

Also, I received a complaint from windows users that they cannot see empty folders in Windows Explorer (compressed folder).
I will check this on windows

@srikanth-lingala
Copy link
Owner

When you extract it with zip4j, are the empty directories extracted?

@srikanth-lingala
Copy link
Owner

And also, with the zip file generated by native OS tool, can you see those empty directories in Ubuntu archive manager?

@branimir-vujicic
Copy link
Author

Hi, when I extract the archive, all folders are there, I'll check how it behaves on windows and let you know.

@srikanth-lingala
Copy link
Owner

Can you also answer this question please:

And also, with the zip file generated by native OS tool, can you see those empty directories in Ubuntu archive manager?

@branimir-vujicic
Copy link
Author

When I create an archive with a native OS tool (for ubuntu Linux 18.04: nautilus -> compress ), I can see empty folders in the archive.

Screenshot from 2020-04-09 23-17-17

@srikanth-lingala
Copy link
Owner

I have generated three test files for testing. Can you please post me the screenshots if the Ubuntu Archive manager is showing empty folders for any of these zip file?

file_only_second_header.zip
file_only_first_header.zip
file_with_both_headers.zip

@branimir-vujicic
Copy link
Author

here are screenshots:
Screenshot from 2020-04-10 07-53-08
Screenshot from 2020-04-10 07-53-34
Screenshot from 2020-04-10 07-53-48

@srikanth-lingala
Copy link
Owner

Can you please check again with this file if it shows empty folders?
test.zip

@branimir-vujicic
Copy link
Author

Now, archive manager shows empty folders :D
Screenshot from 2020-04-10 13-41-54

@srikanth-lingala
Copy link
Owner

This one was created by Keka, and not by zip4j. There are not too many differences between the one created by zip4j and by Keka or ubuntu tool. Not sure why Archive manager decides to skip the ones created by zip4j. Looking into it. It might be that I will send you a few more files to check.

@srikanth-lingala
Copy link
Owner

Can you check these 3 zip files again please?
fileWithoutUtf8AndWithVersionMadeBy.zip
filewithversionmadeby.zip
filewithoututf8.zip

@branimir-vujicic
Copy link
Author

here are screenshot
Screenshot from 2020-04-10 15-21-53
Screenshot from 2020-04-10 15-22-05
Screenshot from 2020-04-10 15-22-11

@srikanth-lingala
Copy link
Owner

I think now I figured out the issue. But I am quite surprised that the archive manager is behaving this way. Basically, zip4j uses utf8 as default charset. Archive manager seems to ignore the directories which are marked as utf8. Strangely it shows files which are utf8, but seems to ignore only the directories if they are utf8.

Do you have utf8 charset support on your os? And also can you please post the screenshot for one more zip file attached below?

test.zip

@branimir-vujicic
Copy link
Author

Screenshot from 2020-04-10 16-51-15

UTF-8 is default character encoding on my OS

@srikanth-lingala
Copy link
Owner

This is a really strange behaviour by Archive Manager. This seems to be a bug in their tool. Zip specification allows using Cp-437 and utf-8 as standard charsets. And zip4j uses utf-8 as its default charset. Apparently, archive manager on Ubuntu seems to ignore directories which use utf-8 encoding but file names does not contain any non-ascii character. This is totally not against the zip specification, and utf-8 encoding can be used as long as the appropriate flag is set, which zip4j sets in this case. One way for zip4j would have been to use cp-437 by default and use utf-8 only when needed (in case of non-ascii characters), but I do not want to change the default behaviour of zip4j now for this bug in Archive manager, especially when such behaviour is not against zip specification. And moreover, the files and folders in the zip are properly extracted, and other tools do not have any problem with viewing and extracting, I will have to mark this as "not-an-issue".

@branimir-vujicic
Copy link
Author

Thank You for Your time and effort.
I will close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants