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

Zip file cannot be unzipped on Windows if the password contains a section sign #328

Closed
barix opened this issue Jun 23, 2021 · 3 comments
Closed
Labels
bug Something isn't working resolved

Comments

@barix
Copy link

barix commented Jun 23, 2021

Hello,

when I create a zip file with a password that contains a section character (§), I cannot unzip that file with the same password when using 7-Zip 19.00 (x64) on Windows. When I use the command line 7z x out.zip (p7zip Version 15.14.1) in cygwin or WinRar I was able to extract all files. I reverted the commit b69843a and was able to create and unzip a zip file with a password that contains a section character.

I think the crucial code section is Zip4jUtil#convertCharArrayToByteArray(char[]). Code after reverting b69843a (was the only merge conflict):

public class Zip4jUtil {
...
  public static byte[] convertCharArrayToByteArray(char[] charArray) {
    byte[] bytes = new byte[charArray.length];
    for (int i = 0; i < charArray.length; i++) {
      bytes[i] = (byte) charArray[i];
    }
    return bytes;
  }
...

convertCharArrayToByteArray("§".toCharArray()) without InternalZipConstants returns "�" and with InternalZipConstants "§".

@barix barix changed the title Zip file cannot be unzipped if the password contains a section sign Zip file cannot be unzipped on Windows if the password contains a section sign Jun 23, 2021
@srikanth-lingala srikanth-lingala added the bug Something isn't working label Nov 10, 2021
@srikanth-lingala
Copy link
Owner

Using non-ascii characters for passwords is in a grey zone as far as zip specification is concerned. Some tools convert passwords to utf8 and some don't. With the change that you linked, zip4j converts the password to utf8 by default, and I guess Windows doesn't, and that's why it works fine in your case when you revert the utf8 conversion.

I added an option to ZipFile api to use utf8 or not for password encoding and decoding. If you are sure that your zip file will only be used on windows, you can now generated the zip files by not using utf8. You can set this flag via ZipFile.setUseUtf8CharsetForPasswords(boolean).

I will include this in the next release

@srikanth-lingala
Copy link
Owner

Fixed in v2.11.0 released today

@jiangxincode
Copy link

test_hun 焰.zip

This zip file is encrypted by "hun 焰", But it can not be decrypted by zip4j like this:

    public static void main(String[] args) {
        byte[] readBuffer = new byte[4096];
        Zip4jConfig zip4jConfig = new Zip4jConfig(StandardCharsets.UTF_8, 4096, false);
        try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream("E:\\Code\\temp\\tmp\\test\\test_hun 焰.zip"), "hun 焰".toCharArray(), zip4jConfig)) {
            while (zipInputStream.getNextEntry() != null) {
                while (zipInputStream.read(readBuffer) != -1) {
                }
            }
            System.out.println("success");
        } catch (ZipException e) {
            System.out.println("fail1");
        } catch (IOException e) {
            System.out.println("fail2");
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working resolved
Projects
None yet
Development

No branches or pull requests

3 participants