srikanth-lingala / zip4j

A Java library for zip files and streams
Apache License 2.0
2.06k stars 310 forks source link

Can not extract file accurately use FileHeader when some file has the same fileName #456

Open lzxshare opened 2 years ago

lzxshare commented 2 years ago

i create SplitZipFiles with some files, and among them some file has the same file name but from different path:

A/B/C/fileName.txt A/F/G/fileName.txt A/B/C/P/fileName.txt E/B/C/fileName.txt ....

i want to extract single file,like A/B/C/P/fileName.txt,and i use this api code:

FileHeader fileHeader = zipFile.getFileHeader("fileName.txt"); zipFile.extractFile(fileHeader, parentFile.getAbsolutePath(), mergeName);

but i got a not correct file.i also use the full path to get FileHeader:

FileHeader fileHeader = zipFile.getFileHeader("A/B/C/P/fileName.tx"); zipFile.extractFile(fileHeader, parentFile.getAbsolutePath(), mergeName);

but they returns null,i debug the source code,and i found Zip FileHeader only show file names,her like this:

image

how can i extract single file accurately? i really need your help,may you give me some helpful ideas?thank you very much

srikanth-lingala commented 2 years ago

I was successfully able to extract with the scenario you mentioned. This is the structure of the files in the zip file:

Folder_Structure

I generated the zip file like this:

ZipFile zipFile = new ZipFile("Test.zip");
zipFile.addFolder(new File("FOLDER_TO_ADD"));

I attached the generated zip file. I extracted the text file with this code:

ZipFile zipFile = new ZipFile("Test.zip");
FileHeader fileHeader = zipFile.getFileHeader("test/a/b/123.txt");
zipFile.extractFile(fileHeader, OUTPUT_DIR);

Test.zip

lzxshare commented 2 years ago

I was successfully able to extract with the scenario you mentioned. This is the structure of the files in the zip file:

Folder_Structure

I generated the zip file like this:

ZipFile zipFile = new ZipFile("Test.zip");
zipFile.addFolder(new File("FOLDER_TO_ADD"));

I attached the generated zip file. I extracted the text file with this code:

ZipFile zipFile = new ZipFile("Test.zip");
FileHeader fileHeader = zipFile.getFileHeader("test/a/b/123.txt");
zipFile.extractFile(fileHeader, OUTPUT_DIR);

Test.zip

yeah,thanks for your response,i will show my prroject code:

    because i have many files,i split the zip file,like this:

    ZipParameters zipParameters = new ZipParameters();
    zipParameters.setEncryptFiles(true);
    zipParameters.setCompressionLevel(CompressionLevel.FAST);
    zipParameters.setIncludeRootFolder(false);
    zipParameters.setSymbolicLinkAction(ZipParameters.SymbolicLinkAction.INCLUDE_LINK_AND_LINKED_FILE);
    zipParameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);  

    ZipFile zipFile = new ZipFile(packOutputFile, keyword.toCharArray());
    zipFile.setRunInThread(true);
    zipFile.createSplitZipFile(backFileList, defaultZipParameters, true, SPLIT_SIZE);

    note: the parameters passed "backFileList"  all is the files, not contains folder
    and here is output zip file,the encryption password is 111111,you can input password to unzip the test file.

    so,whether i got a wrong step code? please help me correct the wrong code settings, thank you very much.

1658803896900.zip :