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

After using zipFile.renameFiles to fix the garbled Chinese filenames in the compressed package, an error message appears during extraction stating "File header and local file header mismatch." #543

Open
nonameShijian opened this issue May 13, 2024 · 0 comments

Comments

@nonameShijian
Copy link

nonameShijian commented May 13, 2024

I am using version 2.11.5 of net.lingala.zip4j:zip4j to unzip a ZIP file. This ZIP file was compressed using the WinRAR software on the Windows 10 system, so its default encoding should be GBK.

Here is the code snippet I'm using:

Map<String, String> fixFileHeaders = new HashMap<>();

private void fixGarbledFileNames() throws ZipException {
    List<FileHeader> headerList = zipFile.getFileHeaders();
    for (FileHeader fileHeader : headerList) {
        if (fileHeader.getExtraDataRecords() != null) {
            for (ExtraDataRecord extraDataRecord : fileHeader.getExtraDataRecords()) {
                long identifier = extraDataRecord.getHeader();
                if (identifier == 0x7075L) {
                    byte[] dataBytes = extraDataRecord.getData();
                    ByteBuffer buffer = ByteBuffer.wrap(dataBytes);
                    byte version = buffer.get();
                    if (version == 1) {
                        String garbledName = fileHeader.getFileName();
                        String correctedName = new String(dataBytes, 5, dataBytes.length - 5, StandardCharsets.UTF_8);
                        fixFileHeaders.put(garbledName, correctedName);
                        break;
                    }
                }
            }
        }
    }
    zipFile.renameFiles(fixFileHeaders);
}

The fixGarbledFileNames method utilizes zipFile.renameFiles to correct the names of files with garbled Chinese characters to their proper form. However, after executing this, when I proceed with "zipFile.extractAll(filePath);" to extract the contents, an error arises: "File header and local file header mismatch."

Error Stack:

net.lingala.zip4j.exception.ZipException: File header and local file header mismatch
at net.lingala.zip4j.tasks.AbstractExtractFileTask.verifyNextEntry(AbstractExtractFileTask.java:173)
at net.lingala.zip4j.tasks.AbstractExtractFileTask.extractFile(AbstractExtractFileTask.java:54)
at net.lingala.zip4j.tasks.ExtractAllFilesTask.executeTask(ExtractAllFilesTask.java:41)
at net.lingala.zip4j.tasks.ExtractAllFilesTask.executeTask(ExtractAllFilesTask.java:17)
at net.lingala.zip4j.tasks.AsyncZipTask.performTaskWithErrorHandling(AsyncZipTask.java:51)
at net.lingala.zip4j.tasks.AsyncZipTask.access$400(AsyncZipTask.java:9)
at net.lingala.zip4j.tasks.AsyncZipTask$1.run(AsyncZipTask.java:36)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
at java.lang.Thread.run(Thread.java:1012)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant