File corrupted after zipping

4 posts, 1 answers
  1. Dario Concilio
    Dario Concilio avatar
    172 posts
    Member since:
    Apr 2016

    Posted 01 Dec 2016 Link to this post

    I'm trying to zip a PDF file, it creates zip file correctly, but if I try to open compressed PDF file, it show me error message "Corrupted file" (I'm trying to open by my PDF reader), where I wrong?

    using (Stream stream = File.Open(fileFullName, FileMode.Create))
                        {
                            using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, false, null))
                            {
                                using (ZipArchiveEntry entry = archive.CreateEntry(fi.Name))
                                {
                                    var writer = new StreamWriter(entry.Open());
     
                                    using (Stream streamFileToZip = File.Open(fi.FullName, FileMode.Open))
                                    {
                                        var buffer = new byte[4096];
                                        int sourceBytes = 0;
                                        do
                                        {
     
                                            sourceBytes = streamFileToZip.Read(buffer, 0, buffer.Length);
                                            writer.Write(buffer);
     
                                        } while (sourceBytes > 0);
     
                                    }
     
                                    writer.Flush();
                                }
                            }
                        }
  2. Dario Concilio
    Dario Concilio avatar
    172 posts
    Member since:
    Apr 2016

    Posted 01 Dec 2016 in reply to Dario Concilio Link to this post

    I resolved first step, but it seems that docx file are crompressed wrong.

    I tryed doc, txt, jpg and pdf. All these files work correctly.

    using (Stream stream = File.Open(fileFullName, FileMode.Create))
    {
        using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, false, null))
        {
            using (ZipArchiveEntry entry = archive.CreateEntry(fi.Name))
            {
                var writer = new BinaryWriter(entry.Open());
     
                using (Stream streamFileToZip = File.Open(fi.FullName, FileMode.Open))
                {
                    var buffer = new byte[4096];
                    int sourceBytes = 0;
                    do
                    {
     
                        sourceBytes = streamFileToZip.Read(buffer, 0, buffer.Length);
                        writer.Write(buffer);
     
                    } while (sourceBytes > 0);
     
                }
     
                writer.Flush();
            }
        }
    }
  3. Answer
    Tanya
    Admin
    Tanya avatar
    583 posts

    Posted 05 Dec 2016 Link to this post

    Hello Dario,

    It seems like the bytes of the file are not correctly written to the produced archive. As the approach you are using is error-prone and hard to maintain, I would suggest you to use the ZipExtensions' methods that provide you a convenient way to create zip archives and entries. Here is a sample in code:
    using (Stream stream = File.Open("sample.zip", FileMode.Create))
    {
        using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, false, null))
        {
            archive.CreateEntryFromFile(PathToSampleDocument,"entry.docx");
        }
    }

    Hope this is helpful.

    Regards,
    Tanya
    Telerik by Progress

  4. Dario Concilio
    Dario Concilio avatar
    172 posts
    Member since:
    Apr 2016

    Posted 06 Dec 2016 in reply to Tanya Link to this post

    Thank you, this is more simple.
Back to Top