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.
}