This is a migrated thread and some comments may be shown as answers.

Corrupt Archive

2 Answers 108 Views
ZipLibrary
This is a migrated thread and some comments may be shown as answers.
Jason
Top achievements
Rank 2
Jason asked on 04 May 2015, 11:09 AM

I need to save a compressed byte stream in to a database table, the problem is that whenever i open the file, winZip/winRar/windows reports that the archive is corrupt. I've tried setting leavOpen on the ZipArchive (as suggested in some other posts) but it make no difference.

any ideas?

        private byte[] Compress(byte[] pData, string pFileName)
        {
            CompressionSettings lSettings = new LzmaSettings();

            using (MemoryStream lMemoryStream = new MemoryStream()) {
                using (ZipArchive lArchive = new ZipArchive(lMemoryStream, ZipArchiveMode.Create, false, null)) {
                    ZipArchiveEntry lAttachment = lArchive.CreateEntry(pFileName, lSettings);                   
                    using (var lAttachmentStream = lAttachment.Open()) {
                        using (var lStreamWriter = new StreamWriter(lAttachmentStream)) {
                            lStreamWriter.Write(pData);
                        }
                    }
                }
                return lMemoryStream.ToArray();
            }
        }

2 Answers, 1 is accepted

Sort by
0
Jason
Top achievements
Rank 2
answered on 04 May 2015, 11:16 AM
I'm using v2015.1.401.45
0
Jason
Top achievements
Rank 2
answered on 04 May 2015, 11:53 AM

so after a lot of trail and error, i managed to fix it.

i suspect that specifying the text encoder might have been the solution.

--Jason

        private byte[] Compress(byte[] pData, string pFileName)
        {
            CompressionSettings lSettings = new LzmaSettings();

            using (MemoryStream lMemoryStream = new MemoryStream()) {
                using (ZipArchive lArchive = new ZipArchive(lMemoryStream, ZipArchiveMode.Create, false, System.Text.Encoding.UTF8)) {
                    ZipArchiveEntry lAttachment = lArchive.CreateEntry(pFileName, lSettings);
                    using (BinaryWriter lWriter = new BinaryWriter(lAttachment.Open())) {
                       lWriter.Write(pData);
                    }
                    lAttachment.Dispose();
                }
                return lMemoryStream.ToArray();
            }
        }

Tags
ZipLibrary
Asked by
Jason
Top achievements
Rank 2
Answers by
Jason
Top achievements
Rank 2
Share this question
or