I want to compress an entire directory which can have any number of subdirectories into a single ZIP file.
I am able to compress a single file into a zip file programmatically.
To compress an entire directory, i can think of a recursive program that walks through each subdirectory and compresses it.
But Is there any simple way to compress the entire folder using the similar code, without having to write any recursive functions?
Using DotNetZip, there's an AddDirectory() method on the ZipFile class that does what you want:
using (var zip = new Ionic.Zip.ZipFile())
{
zip.AddDirectory("DirectoryOnDisk", "rootInZipFile");
zip.Save("MyFile.zip");
}
This example, and many others, are available on codeplex.
Take a look at one of these API's:
DotNetZip
SharpZipLib
ZipFile.CreateFromDirectory(<path of folder you need to zip>, <path of zip file with .zip in the end>, CompressionLevel.Fastest, true);
You can see Article about Zip / Unzip folders and files with C#.
Related
I'm creating a game in which a user can create custom content. There are two files associated with each custom creation: an .ogg file and a .xml file. Previously, I had a folder that contained all of the associated files, but I'd like to wrap all the associated files within a .tar file instead.
Using the following code, I can create a .tar archive (with the custom extension ".krs"):
FileInfo[] filesInDirectory = folder.GetFiles();
string tarArchiveName = #"C:\Users\me\Desktop\UserData\songs\songName.krs";
using (Stream targetStream = new GZipOutputStream(File.Create(tarArchiveName)))
using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(targetStream, TarBuffer.DefaultBlockFactor))
{
foreach (FileInfo file in filesInDirectory)
{
TarEntry entry = TarEntry.CreateEntryFromFile(file.FullName);
tarArchive.WriteEntry(entry, false);
}
}
This doesn't give any errors, but when I open the .krs file as a .tar using 7zip, the the files are buried underneath ALL the parent directories of the original files that were copied to the archive. For example, the path of the "data.xml" file within the .tar file is "C:\Users\me\Desktop\UserData\songs\songName\data.xml".
I want to open the .tar file and there no top-level directory - just the two files. For example the data.xml file within the .tar archive should be simply "data.xml".
I know this is achieveable because I can do it manually using 7zip. How can I do this using the SharpZipLib library in C#? I found this answer that seems to address my problem, but it's written in Python, a language I have no understanding of.
EDIT: I did some more searching and found this answer. I tried the solution, and it took away everything except the first parent directory of the files (".tar : parentFolder\data.xml"). Is it possible to remove that as well to avoid having to do any digging when I extract these files later?
When testing the answer I posted in my edit, I found that when extracting the files, they come out by themselves and do not come in a folder. This is the answer I was looking for.
So i have a zip file in a directory. This zip file contains different kind of files. When a user clicks on a button in my WPF application, the zip file needs to be updated.
It needs to check files from another zip file in another directory. If a file doesnt excist in the first zip file, it needs to copy that file from the other zip file to the new zip file.
i used the Ionic zip methode for this.
So far i just used the file.copy overwrite = true code.
But when the zip file is 1gb+ it is taking very long because it just replaced the zip file.
does someone know how i can resolve this?
Greetings Thomas
UPDATE:
this is the code i got so far:
private void getlocaldata()
{
string admindata = #"\\networklocation\test.zip";
string localPath = #"C:\finaldata\test.zip";
File.Copy(admindata, localPath, true);
}
You can use the DotNetZip library.
Check the file in the zip :
zip["Readme.txt"] = null;
But anyway you must compress files again for changing zips password. You can find examples for that here.
I have a folder containing zip files. I want to unzip them. After unzipping them, I have to find if there are any other zip file found inside the directory. If found, I've to unzip them also. The inner level of the presence of zip files are undetermined. How to unzip all the zip files in sub directories.
It sounds like a fundamentally recursive operation. As Tim indicated above, we can't really give specifics without knowing the library you're using (personally, I'm a fan of Ionic's library), but it would go something like this:
Function Unzip(file as File)
zipfile = ZipLibrary.Load(file);
For Each innerfile as File in zipfile.files
If (innerfile.Name.EndsWith(".zip")) Then
Unzip(innerfile);
End If
End For
End Function
Of course, as with any form of recursion like this, you can potentially save on stack space by building a list of files to be unpacked and adding and iterating through it rather than doing the recursive call. You could also potentially use the zip library itself to check whether a file is a valid zip file if you are uncertain whether it will have the correct extension.
I have a single file, Setup1.cab, which is split up into Setup1.zip.001 and Setup1.zip.002 that I used 7zip to archive. Once those volumes reach their destination, I'd like to be able to use C# to extract that file from both archives into the same directory where they will reside. Is this something that SharpZipLib is capable of, or should I be using another tool?
Otherwise, is there a way to combine the two using C# (or another tool - I'm open!) into one zip file, THEN extract it using SharpZipLib?
Thanks!
EDIT: 7zip will not be installed on the destination machines. Also, I'm open to using a different method of archiving the original file; I just need it to be in chunks of under 500MB, and the original file is 570MB.
I would take a look at the SevenZipSharp library and actually use 7zip via C# to handle the decompression.
how to use zippackage class to know about the content in .zip file?
for ex to know the type of file inside this zip file.
Once you open the .zip file, you can use the GetParts method to get a collection of all the files inside the .zip. See the PackagePart class.
http://sharpdevelop.net/OpenSource/SharpZipLib/ is a good zip library for CSharp.
Although, you might just want to use something like the DeflateStream class (http://msdn.microsoft.com/en-us/library/system.io.compression.deflatestream.aspx) which provides a compressed stream and doesn't require dealing with the complexities of an archive that handles multiple files.