Problems with creating ZIP file in c#
-
13-06-2021 - |
Question
I am trying to create zip file by this code, but nothing works, the constractor of ZipFile doesnt get
() only overloading with arguments, and i don't have the SAVE
method?
Whats wrong?
using (ZipFile zip = new ZipFile())
{
zip.AddEntry("C://inetpub//wwwroot//Files//Wireframes//" + url, zip.Name);
zip.AddDirectory("C://inetpub//wwwroot//Files//Wireframes//" + url);
zip.Save(downloadFileName);
}
Solution
To zip an entire directory with SharpZipLib you could try this method:
private void ZipFolder(string folderName, string outputFile)
{
string[] files = Directory.GetFiles(folderName);
using (ZipOutputStream zos = new ZipOutputStream(File.Create(outputFile)))
{
zos.SetLevel(9); // 9 = highest compression
byte[] buffer = new byte[4096];
foreach (string file in files)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
zos.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int byteRead;
do
{
byteRead = fs.Read(buffer, 0,buffer.Length);
zos.Write(buffer, 0, byteRead);
}
while (byteRead > 0);
}
}
zos.Finish();
zos.Close();
}
As you can see we have a really different code from you example.
As I have said in my comment above, your example seems to come from DotNetZip
If you wish to use that library your code will be:
using (ZipFile zip = new ZipFile())
{
zip.AddFile("C://inetpub//wwwroot//Files//Wireframes//" + url);
zip.AddDirectory("C://inetpub//wwwroot//Files//Wireframes//" + url, "WireFrames");
zip.Save(downloadFileName);
}
EDIT: To add al PNG files in a certain directory
using (ZipFile zip = new ZipFile())
{
string filesPNG = Directory.GetFiles("C://inetpub//wwwroot//Files//Wireframes//" + url, "*.PNG);
foreach(string file in filesPNG)
zip.AddFile(file);
zip.Save(downloadFileName);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow