Corrupt Archive

3 posts, 0 answers
  1. Jason
    Jason avatar
    9 posts
    Member since:
    Aug 2009

    Posted 04 May 2015 Link to this post

    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. Jason
    Jason avatar
    9 posts
    Member since:
    Aug 2009

    Posted 04 May 2015 in reply to Jason Link to this post

    I'm using v2015.1.401.45
  3. Jason
    Jason avatar
    9 posts
    Member since:
    Aug 2009

    Posted 04 May 2015 Link to this post

    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();
                }
            }

Back to Top