sharpcompress icon indicating copy to clipboard operation
sharpcompress copied to clipboard

zip format, Chinese password decompression failed

Open toolgood opened this issue 3 years ago • 3 comments

zip format, Chinese password decompression failed Knowing that the password is "公众号身披御光的盟国" But the decompression failed

Encoding: GB18030 test file: https://github.com/toolgood/OtherFiles/blob/master/SharpCompress/passwordError.zip

        private static bool CheckPassword3(string file, string password, Encoding encoding2, Encoding encoding3)
        {
            ReaderOptions options = new ReaderOptions();
            options.LookForHeader = true;
            options.LeaveStreamOpen = false;
            if (string.IsNullOrWhiteSpace(password) == false) {
                options.Password = password;
            }
            //options.ArchiveEncoding = new ArchiveEncoding(Encoding.GetEncoding("GB18030"), Encoding.GetEncoding("GB18030"));
            options.ArchiveEncoding = new ArchiveEncoding(encoding2, encoding3);

            var archive = ArchiveFactory.Open(file, options);
            try {
                if (archive.IsComplete) {
                    var entry = archive.Entries.Where(entry => !entry.IsDirectory).OrderBy(e => e.CompressedSize).FirstOrDefault();
                    if (entry != null) {
                        using (var ms = new MemoryStream()) {
                            entry.WriteTo(ms);
                        }
                        return true;
                    }
                }
            } catch (Exception e) {
            } finally {
                archive.Dispose();
            }
            return false;
        }

toolgood avatar Aug 29 '20 01:08 toolgood

Use the 7-Zip Extra command line to decompress correctly

7-Zip Extra can be downloaded at https://www.7-zip.org/download.html

D:\Downloads\7z1900-extra>7za e -oD:\Downloads\20200823\jiaoao\存档.无密码 -p公众号身披御光的盟国 D:\Downloads\20200823\jiaoao\passwordError.zip

7-Zip (a) 19.00 (x86) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21

Scanning the drive for archives:
1 file, 188 bytes (1 KiB)

Extracting archive: D:\Downloads\20200823\jiaoao\passwordError.zip
--
Path = D:\Downloads\20200823\jiaoao\passwordError.zip
Type = zip
Physical Size = 188

Everything is Ok

Size:       12
Compressed: 188

toolgood avatar Aug 29 '20 03:08 toolgood

Thanks for the repro steps. I'll look at this soon.

Not sure why encoding has been difficult for me in this project. I assume UTF8 doesn't work?

adamhathcock avatar Aug 29 '20 06:08 adamhathcock

Use the 7-Zip Extra command line to decompress correctly

This doesn't actually work correctly unless you're on a Chinese system anyways (which seems to use GB18030 for Command Prompt, as that's the system page). What gets sent to 7zip is ultimately just a byte array, which on your system is decoded correctly from the string using GB18030. On other systems (like English systems using Windows-1252), it will not work (as those characters are unsupported). That is unless you translate the raw bytes into the user's system page. For Windows-1252, this would be ¹«ÖÚºÅÉíÅûÓù¹âµÄÃ˹ú. As long as the string gets decoded into the correct byte array, it works.

CasualPokePlayer avatar May 10 '23 01:05 CasualPokePlayer