I want to add(and overwrite) all files including other folders that are in single folder.
Example:
In archive zip.zip:
work.docx
1.txt
Photos
|_ocean.png
|_horse.png
After adding files & folder from another folder it would be like:
work.docx
1.txt
code.cs
Photos
|_ocean.png
|_horse.png
Program
|_program.exe
|_config.txt
I tried to do that with Ionic's zip library:
Ionic.Zip.ZipFile zf = Ionic.Zip.ZipFile.Read(#"C:\\zip.zip");
zf.UpdateDirectory(#"C:\\Program");
zf.Save();
It works, but it doesn't overwrite files.
If you are using .NET 4.5 then you can use
System.IO.Compression.ZipArchive
It allows a single step compression.
ZipFile.CreateFromDirectory(#”C:\zip”, #”C:\zip.zip”);
To update it you can use ZipArchiveMode.Update
Related
I am trying to zip a folder with all its files and folders but its not working properly.
Here is the Folder i am trying to Zip it contains some files and a subfolder
and here is how i am doing it.
System.IO.Compression.ZipFile.CreateFromDirectory(DestPath, ZipPath,System.IO.Compression.CompressionLevel.Optimal,true);
but the result is this
It does not include xml file and the subfolder. Expected output should be like this
Is it possible to achieve without using an external library ?
I want to zip some files in a directory. I have googled a lot but i always saw examples about zipping a directory (all files in directory) by the code below:
string startPath = #subPath;
string zipPath = #"C:\result.zip";
ZipFile.CreateFromDirectory(startPath, zipPath);
I am referencing System.IO.Compression.Filesystem.dll
This code zips all the files in the directory referenced by startPath.
I tried ZipArchive class for compressing but it compress file with directories on its path. For example,
assume i want to zip "a.txt" located at "C:\directories\Graphics\a.txt". The zip file contains directories "directories" and "Graphics" and into these directories the file "a.txt". It is not a good solution to cut files then erase the directories and paste again since i am working on big size data.
How can I zip only some of files at a directory?
How can I zip some files that are located in different directories?
I am looking up the subject for 2 days. Is it a kind of constraint in .Net?
You can use the ZipArchive class, first create it and then - once created you just add ZipArchiveEntry objects into the archive. Each of the ZipArchiveEntry objects represents a single file that you will add to the archive.
Some tips:
You can find more info and a sample of how this could be done on MSDN, here: http://msdn.microsoft.com/pl-pl/library/system.io.compression.ziparchive(v=vs.110).aspx and here: http://msdn.microsoft.com/pl-pl/library/system.io.compression.ziparchiveentry(v=vs.110).aspx.
This may not be an exact solution but should work for your scenario. I'll give you the steps :
Create a Temporary Directory
Read and copy your Selected files into the Temporary directory. This applies to your second scenario as well.
Zip the temporary directory
Delete Temporary Directory after you are done
I would recommend using File.Copy to copy all your desired files to a temporary directory, then zip them all from there and delete the temporary directory once the zip operation has finished.
I'm developing a 3D application in which the user may load multiple images, the 3D library I use has a function to export the whole scene as XAML, of course in XAML I only have the path to the images, so the file will only be valid on the machine in which it has been created, what I want to do is to save the XAML , change image(s) path inside the XAML to be relative to the XAML file path, save image(s) along the XAML in the same new file, like this the new file will always contain the XAML and the images. how to save multiple files types within the same file ? any other suggestions on the whole idea would be appreciated.
If you are using .NET 4.5, then you can programmatically save multiple files into a zip folder easily using the new ZipFile class.
First, you'll need to prepare your files into a new folder (Directory.CreateDirectory, File.Move) and then you can simply use the ZipFile.CreateFromDirectory Method (adapted from the linked page):
string filePathOfNewFolder = #"c:\example\start";
string zipFilePath = #"c:\example\result.zip";
ZipFile.CreateFromDirectory(filePathOfNewFolder, zipFilePath);
UPDATE >>>
For .NET 4, you might have to rely more on third party libraries. You can download the DotNetZip library from the DotNetZip - Zip and Unzip in C#, VB, any .NET language page on CodePlex. It also works fairly simply. Here is an example taken from the linked page:
using (ZipFile zip = new ZipFile())
{
// add this map file into the "images" directory in the zip archive
zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images");
// add the report into a different directory in the archive
zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files");
zip.AddFile("ReadMe.txt");
zip.Save("MyZipFile.zip");
}
Store your XAML and other image files into one file (e.g. zip or cab).
When running your program, unzip relevant file into a temp folder so you can get the relevant path working.
Empty the temp folder afterwards if required.
I want zip determinated folders.
Actually I have a folder named MyFolders, and behind I've multiples folders and files, and I would use CreateFromDirectory method but zip all, but only I want zip 2 folders: "Tools" and "Data".
Due to this, I create a temporary directory and use CreateFromDirectory, but I don't want copy the same folders to other location in the disk (almost, the zipped already folders is better).
So, is it possible?
I want do something like this:
ZipFile my_zip;
my_zip.AddDirectory("Tools");
my_zip.AddDirectory("Data");
but in the .Net Framework 4.5.1, and without external libraries.
Thank you very much!
I need to zip a file in the folder c:\projects\test\test.log
I need to zip the test.log to a zip folder.I am using Ionic.zip.dll.
when I zipped the folder, it is entirely adding the folder c:\projects\test\test.log.
I need to add the test.log to a zip folder
for eg.jan.zip. it should contain only one file. no folder structure needed.
Thanks
Instead of
zipFile.AddFile(filename);
you should use
zipFile.AddFile(filename, "");
That will insert the file at the root of the zip file, ie without a path.