Zip a folder having subfolders using zipstorer class - c#

I am using ZIpstorer class library to compress files. I am able to zip a file by calling zip.AddFile procedure. But I want to zip folders which has subfolders too. So the resulting zip file should have folder and subfolder structure inside the zip file normally. I am not able to do it. if i process each file by looping all folders and sub folders and then if call zip.Addfile then it will result in zip file with all the files inside it without a directory structure.
So how can i zip a folder using Zipstorer class . What changes i have to do?

You can use (back)slashes for the _filenameInZip (sic) parameter to add files in a directory in the zip:
zip.Addfile(,,"directory/filename.txt",);
Or
zip.Addfile(,,"directory\\filename.txt",);

Related

How to unzip .zip.001, .zip.002, zip.003... multiple files in C#?

I zipped one big file into multiple zip files using 7z tool.
It generated xxx.zip.001, xxx.zip.002, xxx.zip.003. so .zip.001 would be parent zip file.
I tried to unzip using SevenZipExtractor.1.0.17. It unzipped the files from the parent file only, but I needed all unzipped.
Actually, when I used WinZip tool, it generate like xxx.zip, xxx.z01, xxx.z02. these all files could be unzipped when I unzipped xxx.zip file.
Is there any library or way for doing this?

Zip a parent directory with files and child directories in .NET Framework

Is there a way to ONLY zip files and directories at the same time. I was using ZipArchive but is not working for Directories. The directory that I want to add in the Zip file has more directories and a zip file inside, so it's a nested directory. Can someone please assist.
using (ZipArchive zipArchive = ZipFile.Open(zipFilePath, ZipArchiveMode.Create))
{
foreach (FileInfo file in filesToZip)
{
zipArchive.CreateEntryFromFile(Path.Combine(filePath, file.Name), file.Name);
}
}
}
Have you tried ZipFile.CreateFromDirectory?
ZipFile.CreateFromDirectory(filePath, zipFilePath);
If you need to zip up an entire directory (including sub-folders), this may be simpler.
To create an empty file or directory, use CreateEntry(String)
Include the slash for directories
zipArchive.CreateEntry(#"DirectoryName\");

Adding a whole folder (with subfolders) as embedded resource?

I have an application that upon execution It copies two folders with subfolders that are in the same location to another windows location %AppData%
Now I have the following files :
MyApp.exe , Folder1, Folder2
In each folder there are subfolders. How to embed these two folders as resources inside the application so after compiling the program, I get only one executable file. And when I click on it, it extract the two folders to the same location then do the rest of job.
I know how to add a file as embedded resource then retrieve it using reflection,
but how about a folder Is that even possible??
I had to solve this problem recently. I embedded a ZIP file, and then decompressed it at runtime.
.NET 4.5 includes ZIP functionality. If not, use SharpZipLib or DotNetZip.

create zip file with folder structure with c#

i have a folder structure and files in those folders.i have to zip all those files with folder structure.
i found examples which only zip a file not all folder structure. i am working on VS 2008. please suggest me the way to zip files with folder structure with c#
Have you tried DotNetZip ?
You can use it to zip a folder.
actually i dont have to create folders first. it creates folder automatically when i specify files with folder name. and it works with both the links provided. thanks guys

Add files to ZIP without paths, using SharpZipLib

I need to combine 3 files into 1 zip file and make it available to download for the user. I am able to achieve my requirement except one thing: it zips the files into the subfolders.
For example, my files are located like the following:
C:\TTCG\WebSites\Health\ABC.CSV
C:\TTCG\WebSites\Health\XYZ.CSV
C:\TTCG\WebSites\Health\123.CSV
But in the zip file, it zip the files in the folder by using "TTCG\WebSites\Health\" as the path. Please see the attach file.
I don't want the folders in the path. I just want 3 files in the zip file without folders. How can I achieve that?
My codes to generate the zip file is as below:
ZipFile z = ZipFile.Create(Server.MapPath("~" + #"\Accident.zip"));
//initialize the file so that it can accept updates
z.BeginUpdate();
//add the file to the zip file
z.Add(Server.MapPath("~" + #"\ABC.csv"));
z.Add(Server.MapPath("~" + #"\XYZ.csv"));
z.Add(Server.MapPath("~" + #"\123.csv"));
//commit the update once we are done
z.CommitUpdate();
//close the file
z.Close();
Based on the FAQ, you have to strip the folder path out manually:
How can I create a Zip file without folders?
Remove the path portion of the filename used to create a ZipEntry
before it is added to a ZipOutputStream
ZipEntry entry = new ZipEntry(Path.GetFileName(fullPath));
The FAQ can be found here.
It seems to be a limitation of the library. Hope this helps!
If you have your files in a FileSystemInfo, you can use: z.Add(file.FullName, Path.GetFileName(file.FullName));
This will add your files in the root directory of your zip.
z.Add(pathToFile, pathInZipFile);

Categories

Resources