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

File corrupted after zipping

3 Answers 344 Views
ZipLibrary
This is a migrated thread and some comments may be shown as answers.
Dario Concilio
Top achievements
Rank 2
Dario Concilio asked on 01 Dec 2016, 11:25 AM

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

3 Answers, 1 is accepted

Sort by
0
Dario Concilio
Top achievements
Rank 2
answered on 01 Dec 2016, 03:44 PM

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();
        }
    }
}
0
Accepted
Tanya
Telerik team
answered on 05 Dec 2016, 04:56 PM
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 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

0
Dario Concilio
Top achievements
Rank 2
answered on 06 Dec 2016, 09:20 AM
Thank you, this is more simple.
Tags
ZipLibrary
Asked by
Dario Concilio
Top achievements
Rank 2
Answers by
Dario Concilio
Top achievements
Rank 2
Tanya
Telerik team
Share this question
or