Can't open Zip archive #202

Closed
opened 2026-01-29 22:08:17 +00:00 by claunia · 7 comments
Owner

Originally created by @Sigurd2656 on GitHub (Jun 26, 2017).

I'm passing a memory stream which I get from byte array which is zip archive with one MS word file. It works fine if I compress this file to *.7z or *.rar.

var archive = ArchiveFactory.Open(archiveMemStream);

On this step I get InvalidOperationException. Additional information: Cannot determine compressed stream type. Supported Archive Formats: Zip, GZip, Tar, Rar, 7Zip, LZip

Originally created by @Sigurd2656 on GitHub (Jun 26, 2017). I'm passing a memory stream which I get from byte array which is zip archive with one MS word file. It works fine if I compress this file to *.7z or *.rar. `var archive = ArchiveFactory.Open(archiveMemStream);` On this step I get InvalidOperationException. Additional information: Cannot determine compressed stream type. Supported Archive Formats: Zip, GZip, Tar, Rar, 7Zip, LZip
claunia added the question label 2026-01-29 22:08:17 +00:00
Author
Owner

@adamhathcock commented on GitHub (Jun 26, 2017):

Need more code and probably a sample file.

Are you resetting the memory streams position after loading it?

@adamhathcock commented on GitHub (Jun 26, 2017): Need more code and probably a sample file. Are you resetting the memory streams position after loading it?
Author
Owner

@Sigurd2656 commented on GitHub (Jun 27, 2017):

Well, no. Now I do and it works fine with zip as well. It's strange since other formats worked.

Here is the simplified test code I worked with, I'll leave it for history.

`public static IEnumerable DecompressArchive(byte[] compressedBytes)
{
var outputFiles = new List();

        using (var archiveMemStream = new MemoryStream())
        {
            archiveMemStream.Write(compressedBytes, 0, compressedBytes.Length);
            archiveMemStream.Seek(0, SeekOrigin.Begin);
            var archive = ArchiveFactory.Open(archiveMemStream);

            foreach (var entry in archive.Entries.Where(e => !e.IsDirectory))
            {
                var fileName = Path.GetFileName(entry.Key);

                if (string.IsNullOrEmpty(fileName)) continue;

                using (var singleFileSrteam = new MemoryStream())
                {
                    entry.WriteTo(singleFileSrteam);
                    var buf = singleFileSrteam.GetBuffer();
                    outputFiles.Add(new DecompressedFile(buf, fileName));
                }
            }
        }
        return outputFiles;
    }`

And thank you for great product.

Edit: I've noticed that decompressed file size is slightly larger than if it was decompressed conventional way with winrar or 7zip. But file is not corrupted and opens without errrors.

@Sigurd2656 commented on GitHub (Jun 27, 2017): Well, no. Now I do and it works fine with zip as well. It's strange since other formats worked. Here is the simplified test code I worked with, I'll leave it for history. `public static IEnumerable<DecompressedFile> DecompressArchive(byte[] compressedBytes) { var outputFiles = new List<DecompressedFile>(); using (var archiveMemStream = new MemoryStream()) { archiveMemStream.Write(compressedBytes, 0, compressedBytes.Length); archiveMemStream.Seek(0, SeekOrigin.Begin); var archive = ArchiveFactory.Open(archiveMemStream); foreach (var entry in archive.Entries.Where(e => !e.IsDirectory)) { var fileName = Path.GetFileName(entry.Key); if (string.IsNullOrEmpty(fileName)) continue; using (var singleFileSrteam = new MemoryStream()) { entry.WriteTo(singleFileSrteam); var buf = singleFileSrteam.GetBuffer(); outputFiles.Add(new DecompressedFile(buf, fileName)); } } } return outputFiles; }` And thank you for great product. **Edit**: I've noticed that decompressed file size is slightly larger than if it was decompressed conventional way with winrar or 7zip. But file is not corrupted and opens without errrors.
Author
Owner

@adamhathcock commented on GitHub (Jun 27, 2017):

Make sure you're not writing extra bytes to the file. You don't need to write to a memory stream usually.

@adamhathcock commented on GitHub (Jun 27, 2017): Make sure you're not writing extra bytes to the file. You don't need to write to a memory stream usually.
Author
Owner

@Sigurd2656 commented on GitHub (Jun 28, 2017):

Yeah, I figured out that buffer obviously has some extra bytes and fixed that. I'm using stream because I have to write directly to DB, without using HDD, since I get those zips and rars through http GETs as byte arrays.

And one more issue. I've heard how bad zip's locale situation is, my filenames inside zip have Cyrillic characters in their names and I've tried all known to me encodings for ArchiveEncoding.Default property, but entry.Key is still malformed.

Example file link.
www.e-disclosure.ru/portal/FileLoad.ashx?Fileid=1175800

@Sigurd2656 commented on GitHub (Jun 28, 2017): Yeah, I figured out that buffer obviously has some extra bytes and fixed that. I'm using stream because I have to write directly to DB, without using HDD, since I get those zips and rars through http GETs as byte arrays. And one more issue. I've heard how bad zip's locale situation is, my filenames inside zip have Cyrillic characters in their names and I've tried all known to me encodings for ArchiveEncoding.Default property, but entry.Key is still malformed. Example file link. [www.e-disclosure.ru/portal/FileLoad.ashx?Fileid=1175800](url)
Author
Owner

@adamhathcock commented on GitHub (Jun 28, 2017):

Are you sure the file is encoding properly? Just need sharpcompress to decode properly?

@adamhathcock commented on GitHub (Jun 28, 2017): Are you sure the file is encoding properly? Just need sharpcompress to decode properly?
Author
Owner

@Sigurd2656 commented on GitHub (Jun 28, 2017):

Well, I made it. All I had to do is
SharpCompress.Common.ArchiveEncoding.Default = Encoding.GetEncoding(866);
for .zip archives with russian filenames inside. Which is Cyrillic DOS (yes, that old) codepage.
The problem was that zip still uses DOS codepages to encode filenames.

Now, instead of ��⠢ �� ����஬ ������।������ ������.pdf
I get normal Устав АО Газпром газораспределение Обнинск.pdf

And thanks for the help.

@Sigurd2656 commented on GitHub (Jun 28, 2017): Well, I made it. All I had to do is `SharpCompress.Common.ArchiveEncoding.Default = Encoding.GetEncoding(866);` for .zip archives with russian filenames inside. Which is Cyrillic DOS (yes, that old) codepage. The problem was that zip still uses DOS codepages to encode filenames. Now, instead of `��⠢ �� ����஬ ������।������ ������.pdf` I get normal `Устав АО Газпром газораспределение Обнинск.pdf` And thanks for the help.
Author
Owner

@adamhathcock commented on GitHub (Jun 28, 2017):

Glad it worked out

@adamhathcock commented on GitHub (Jun 28, 2017): Glad it worked out
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#202