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

Zip a folder that contains pdf files

3 Answers 391 Views
ZipLibrary
This is a migrated thread and some comments may be shown as answers.
Dario
Top achievements
Rank 2
Dario asked on 20 Jul 2015, 11:12 AM

Hi guys,

this is my problem, I want to zip a folder that contains several pdf files.

I can do it! Ok, I created a zip that contains my pdf files.

But I found all pdf files corrupted, some files I can open it, but it has white page!

I attach my 2 project: 1st is my little library, 2nd it my test project

Thi is my code,

My little library and test project console:

01.//My method of my little class library
02.public void CreateZipToAllFiles(string parFolderToArchive,
03.                                string parZipName,
04.                                string parDestinationZipFile)
05.{
06.    LastError = "";
07. 
08.    try
09.    {
10.        //Apro lo il canale per la creazione del file zip
11.        using (Stream stream = File.Open(string.Format(@"{0}\{1}",
12.                                         parDestinationZipFile,
13.                                         parZipName), FileMode.Create))
14.        {
15.            //Credo l'archivio logico di compressione
16.            using (ZipArchive archive = new ZipArchive(stream,
17.                                                       ZipArchiveMode.Create, false, null))
18.            {
19.                //Leggo dalla cartella i file trovati
20.                foreach (var fileInfo in new DirectoryInfo(parFolderToArchive).GetFiles())
21.                {
22.                    //Creo un movimento per ogni file trovato
23.                    using (ZipArchiveEntry entry = archive.CreateEntry(fileInfo.Name))
24.                    {
25.                        using (StreamReader sr = new StreamReader(fileInfo.FullName, true))
26.                        {
27.                            //Archivio il file corrente nel file zip
28.                            StreamWriter writer = new StreamWriter(entry.Open());
29. 
30.                            while (sr.Peek() >= 0)
31.                                writer.Write((char)sr.Read());
32.                             
33.                            writer.Flush();
34.                        }
35.                    }
36.                }
37.            }
38.        }
39.    }
40.    catch (Exception ex)
41.    {
42.        StringBuilder sb = new StringBuilder("ZipForNav.CreateZipToAllFiles");
43.        sb.AppendLine(ex.Message);
44. 
45.        if(ex.InnerException!=null)
46.        {
47.            sb.AppendLine(ex.InnerException.Message);
48.        }
49. 
50.        LastError = sb.ToString();
51.    }
52.}
53. 
54.//Test by project console
55.static void Main(string[] args)
56.{
57.    Zip.ZipForNav zip = new Zip.ZipForNav();
58.    zip.CreateZipToAllFiles(@"C:\Temp\Folder to zip", "myzipfile.zip", @"C:\Temp\Destination folder");
59. 
60.    if (zip.LastError != "")
61.        Console.Write(zip.LastError);
62.    else
63.        Console.Write("OK");
64. 
65.    Console.ReadLine();
66.}

3 Answers, 1 is accepted

Sort by
0
Nikolay Demirev
Telerik team
answered on 22 Jul 2015, 02:01 PM
Hello Dario,

I have reviewed your code and fixed the issue causing it not to work. Here is the modified code:
01.public void CreateZipToAllFiles(string parFolderToArchive,
02.                                string parZipName,
03.                                string parDestinationZipFile)
04.{
05.    try
06.    {
07.        //Apro lo il canale per la creazione del file zip
08.        using (Stream stream = File.Open(string.Format(@"{0}\{1}",
09.                                         parDestinationZipFile,
10.                                         parZipName), FileMode.Create))
11.        {
12.            //Credo l'archivio logico di compressione
13.            using (ZipArchive archive = new ZipArchive(stream,
14.                                                       ZipArchiveMode.Create, false, null))
15.            {
16.                //Leggo dalla cartella i file trovati
17.                foreach (var fileInfo in new DirectoryInfo(parFolderToArchive).GetFiles())
18.                {
19.                    //Creo un movimento per ogni file trovato
20.                    using (ZipArchiveEntry entry = archive.CreateEntry(fileInfo.Name))
21.                    {
22.                        using (var inputFile = File.Open(fileInfo.FullName, FileMode.Open))
23.                        {
24.                            Stream entryStream = entry.Open();
25.                            inputFile.CopyTo(entryStream);
26.                        }
27.                    }
28.                }
29.            }
30.        }
31.    }
32.    catch (Exception ex)
33.    {
34.        StringBuilder sb = new StringBuilder("ZipForNav.CreateZipToAllFiles");
35.        sb.AppendLine(ex.Message);
36. 
37.        if (ex.InnerException != null)
38.        {
39.            sb.AppendLine(ex.InnerException.Message);
40.        }
41.    }
42.}

I hope this helps.

Regards,
Nikolay Demirev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Dario
Top achievements
Rank 2
answered on 22 Jul 2015, 02:44 PM

Hello Nicolay,

thak you for support, but I don't find a CopyTo method.

InputFile variable is a FileStream that has not this method.

Excuse me, but I forgot to tell you that I'm using a Framework 3.5 (little detail.....)

0
Nikolay Demirev
Telerik team
answered on 22 Jul 2015, 03:42 PM
Hi Dario,

Here is the modified code which works NET Framework 3.5:

01.public static void CreateZipToAllFiles(string parFolderToArchive,
02.                                string parZipName,
03.                                string parDestinationZipFile)
04.{
05.    try
06.    {
07.        //Apro lo il canale per la creazione del file zip
08.        using (Stream stream = File.Open(string.Format(@"{0}\{1}",
09.                                         parDestinationZipFile,
10.                                         parZipName), FileMode.Create))
11.        {
12.            //Credo l'archivio logico di compressione
13.            using (ZipArchive archive = new ZipArchive(stream,
14.                                                       ZipArchiveMode.Create, false, null))
15.            {
16.                //Leggo dalla cartella i file trovati
17.                foreach (var fileInfo in new DirectoryInfo(parFolderToArchive).GetFiles())
18.                {
19.                    //Creo un movimento per ogni file trovato
20.                    using (ZipArchiveEntry entry = archive.CreateEntry(fileInfo.Name))
21.                    {
22.                        using (var inputFile = File.Open(fileInfo.FullName, FileMode.Open))
23.                        {
24.                            Stream entryStream = entry.Open();
25.                            CopyStreamTo(inputFile, entryStream);
26.                        }
27.                    }
28.                }
29.            }
30.        }
31.    }
32.    catch (Exception ex)
33.    {
34.        StringBuilder sb = new StringBuilder("ZipForNav.CreateZipToAllFiles");
35.        sb.AppendLine(ex.Message);
36. 
37.        if (ex.InnerException != null)
38.        {
39.            sb.AppendLine(ex.InnerException.Message);
40.        }
41.    }
42.}
43. 
44.public static void CopyStreamTo(Stream source, Stream destination)
45.{
46.    byte[] numArray = new byte[4096];
47.    while (true)
48.    {
49.        int num = source.Read(numArray, 0, numArray.Length);
50.        int num1 = num;
51.        if (num == 0)
52.        {
53.            break;
54.        }
55.        destination.Write(numArray, 0, num1);
56.    }
57.}


Regards,
Nikolay Demirev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
ZipLibrary
Asked by
Dario
Top achievements
Rank 2
Answers by
Nikolay Demirev
Telerik team
Dario
Top achievements
Rank 2
Share this question
or