Zip files using ZipFiles - c#

say if I have the following:
using (ZipFile zip = new ZipFile())
{
zip.AddFile("C:\\ReadMe.txt");
zip.AddFile("C:\\7440-N49th.png");
zip.AddFile("C:\\2008_Annual_Report.pdf");
zip.Save("C:\\Files\\ZipFiles\\Test.zip");
}
What I am confused on is that when I open up the zip file,
I it goes to C: first then I have to click on Files then I have to click on ZipFiles to see what is in the zip file.
Why doesn't it just open up the files I added when I click on the zip file?

are you using DotNetZip? and do you mean you want to add the files to the root of the zip file? If so, maybe you're trying to do
zip.AddFile("C:\\ReadMe.txt", "");
with the second string being the path you add it to in the zip(empty string if root).

Related

Opening Split Zip Files With DotNetZip

I am trying to extract files from zip files using the DotNetZip library. I am able to extract files when it is a single .zip file. However, when I try to extract files from a multi volume zip file like Something.zip.0 or Something.zip.1, I get the following two exceptions:
-Exception thrown: 'Ionic.Zip.BadReadException' in Ionic.Zip.dll
-Exception thrown: 'Ionic.Zip.ZipException' in Ionic.Zip.dll
Is it possible for DotNetZip to read these type of files, or should I be looking into an alternative approach? I am working on Visual Studios using C#.
Here's a snippet of how I implement my zip file extraction.
using (Ionic.Zip.ZipFile zip = Ionic.Zip.ZipFile.Read(_pathToZip))
{
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestSpeed;
foreach(Ionic.Zip.ZipEntry ze in zip)
{
string fileName = ze.FileName;
bool isThereItemToExtract = isThereMatch(fileName.ToLower(), _folderList, _fileList);
if (isThereItemToExtract)
{
string pathOfFileToExtract = (_destinationPath + "\\" + ze.FileName).Replace('/', '\\');
string pathInNewZipFile = goUpOneDirectoryRelative(ze.FileName);
ze.Extract(_destinationPath, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently);
_newZip.AddItem(pathOfFileToExtract, pathInNewZipFile);
}
}
_newZip.Save();
}
Please refer the DotNetZipLibrary code examples:
using Ionic.Zip;
private void MyExtract(string zipToUnpack, string unpackDirectory)
{
using (ZipFile zip1 = ZipFile.Read(zipToUnpack))
{
// here, we extract every entry, but we could extract conditionally
// based on entry name, size, date, checkbox status, etc.
foreach (ZipEntry e in zip1)
{
e.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
}
}
}
This method should be able to extract either split and not split zip files.
Every zip entry will be extracted with its full path as specified in the zip archive, relative to the current unpackDirectory.
There's no need to check if zip entry exsists (isThereItemToExtract). Interating the zip entries with foreach should do the job.
To avoid collisions you need to check if file with same name as zipEntry exsists in the unpackDirectory, or use ExtractExistingFileAction.OverwriteSilently flag.
Is it possible for DotNetZip to read these type of files, or should I be looking into an alternative approach? I am working on Visual Studios using C#.
In my experience, this is the best library to deal with split zip files.

DotNetZip (Ionic.Zip.dll) doesn't work with compress Chinese File or Folder name

It is very perfect condition when I use as following code to compress English file or folder name,However I do have got serious problem when the name change from English to Chinese name , it isn't work.
How can i do ????
using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(#"C:\inetpub\wwwroot\a"); // Zip folder included all branch files
zip.Save(#"C:\inetpub\wwwroot\Projectzip.zip");//location and name for creating zip file
}
I've found answer to deal with my problem
using (ZipFile zip = new ZipFile(System.Text.Encoding.Default))
thanks

Adding files into a folder inside a zip file in c#

My code to zip files is as follows
ZipArchive zip = ZipFile.Open(destToZip, ZipArchiveMode.Create);
zip.CreateEntry("pubEd/");
string[] fileEntries = Directory.GetFiles(dirToZip);
foreach (string fileName in fileEntries)
zip.CreateEntryFromFile(fileName,Path.GetFileName(fileName), CompressionLevel.Optimal);
zip.Dispose();
In the second line of the code once after creating a zip file I create a folder with a name pubEd inside the zip file.
In the next line I am adding files to the zip folder.
What is happening is files get added to the zip directly.
I want to add these files inside the directory which I created inside the zip.
How do I do that?
By the looks of it you would changePath.GetFileName(fileName) to "pubEd/" + Path.GetFileName(fileName). And get rid of the second line. Thats just based on my reading of the documentation. I have not actually tried it.

How can i add a whole folder to a zip archive

I tried the zip.AddDirectory thing, but I can't figure out how to import a whole folder to an archive(Using DotNetZip).
I do NOT want this:
But this:
From the DotNetZip source code examples :
Remap directories. Zip up a set of files and directories, and re-map them into a different directory hierarchy in the zip file:
using (ZipFile zip = new ZipFile())
{
// files in the filesystem like MyDocuments\ProjectX\File1.txt , will be stored in the zip archive as backup\File1.txt
zip.AddDirectory(#"MyDocuments\ProjectX", "backup");
// files in the filesystem like MyMusic\Santana\OyeComoVa.mp3, will be stored in the zip archive as tunes\Santana\OyeComoVa.mp3
zip.AddDirectory("MyMusic", "tunes");
// The Readme.txt file in the filesystem will be stored in the zip archive as documents\Readme.txt
zip.AddDirectory("Readme.txt", "documents");
zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G") ;
zip.Save(ZipFileToCreate);
}

Problem in using Ionic.Zip namspace

I am using Ionic.zip to zip up the files.
You consider there are 2 files in c:\img\a.txt and b.txt. When zip up these files as following
using (ZipFile zip = new ZipFile())
{
zip.AddItem(#"F:\imp\a.txt");
zip.AddItem(#"F:\imp\b.txt");
zip.AddItem(#"F:\imp\lookup.ini");
zip.AddItem(#"F:\imp\lookups.mdb");
zip.Save("Lookups.zip");
}
It is creating lookups.zip file correctly. But the problem is content of zip file is...
there is one directory named as imp and it is contains those 2files.
But i do not need of the directory entry only i need files which i added.
Please help me.
As shown in the documentation, use the overload which accepts two strings - one for the directory in the zip file:
using (ZipFile zip = new ZipFile())
{
zip.AddItem(#"F:\imp\a.txt", "");
zip.AddItem(#"F:\imp\b.txt", "");
zip.AddItem(#"F:\imp\lookup.ini", "");
zip.AddItem(#"F:\imp\lookups.mdb", "");
zip.Save("Lookups.zip");
}
(That looks like 4 files to me, not 2, and in imp rather than img... but never mind.)

Categories