ZIP determinated folders in a directory - c#

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!

Related

Add all folder items into archive using c#

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

Zip a folder with all its files and subfolders in it C# (DotNet 4.5)

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 ?

How to zip selected files? not all the files in a directory in c#

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.

How to Use a Windows App with Embeded files and folders to copy to a destination?

I am trying to write a small application, whose only purpose is to copy some folders and .cs source files into a user specified Directory, I can do it easy enough by simply having the application look for the files and folders in its own install directory then copy them to thier destination Directory, but I was wondering if its possible to Embed the Folders and Files into the Application, so that when you run the application it creates or copies the folders and files from the exe app directly to the install directory, rather than searching for them in the apps install directory then copying them over. Basically Im trying to only have a single exe file rather than having an exe file and a bunch of folders and files along side it.
Is this possible to do with just a Windows Form App without using an actual Installer Class?
Yes. Embed the files into the application executable as embedded resources. Then when your application runs, access the embedded files and write them to disk in the desired directory structure.
Here is an example of how to embed and access embedded resources from your application assembly.
http://support.microsoft.com/kb/319292
Sure you can, use the BuildAction property as Content or Resource.
Depending on the number and structure of files/folders, you may also consider embedding one zip file and extracting it with sharpziplib or some such.

Unzipping on the fly in C#

I have built a console application that works okay when it references a .exe file from a Program Files, but my users may not have that .exe in their Program Files directory.
I would prefer to keep the package as a single .exe for simplicity, so I was wondering how I can combine the two .exe's into one package.
One thing I thought of is zipping the .exe from the Program Files directory to a temporary location, and I would store the binary data for the zip archive in my console applications source code. Is this the best way to do it or are there better methods?
Note I do not have the source code of the .exe I want to reference in my console application.
You can certainly store extra files in your .exe (or .dll) as embedded resources. Simply change the "build action" for the item in the project to "Embedded Resource". You can retrieve the contents of the file (which could be compressed, if you wished) by using the following:
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("stream name")
You could extract the file onto disk to be able to reference it, or you could load it directly with one of the Assembly.Load() variants, so you wouldn't need to ever store it on disk.
Note that if you do choose to extract it and store it on disk, you'll need administrator permissions on Vista and Windows 7 (and properly administered XP) operating systems in order to save the file(s) to the Program Files directory.
You can use GZipStream to compress and decompress files in C#. For more complex compression, you can use other Zip libraries like SharpZipLib.
Take a look at this link: Embedding assemblies inside another assembly
Basically, ILMerge will do what you are asking.

Categories