I'm trying to zip several folders with their content into one zip file using Ionic Zip Library. The code below did created a zip file, however the folders were not added to it.
private void ZipFolder(List <string> folders, string pathToSaveZipFile)
{
using (ZipFile zip = new ZipFile())
{
foreach (string itrFolder in folders)
{
zip.AddDirectory(itrFolder);
}
zip.Save(pathToSaveZipFile);
}
}
Thank you
AddDirectory(string) adds the files to the root, you need to use
zip.AddDirectory(itrFolder, new DirectoryInfo(itrFolder).Name);
The second argument specifies the folder name within the ZIP.
Related
So I finally got this to work and download my pdf files into a zip file. See below:
List<string> manypaths = (List<string>)TempData["temp"];
var startpath = manypaths;
using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
{
zip.AddFiles(manypaths);
MemoryStream output = new MemoryStream();
zip.Save(output);
return File(output.ToArray(), "application/zip");
}
So the manypaths list variable is holding onto a few paths to the pdf file which are like \\\\ost-stji01\pdfstorage\deposit\02_29_2019.pdf
So when the user downloads these files they have to click through multiple folder structure i.e ost0ji01 > pdfstorage > desposit > then they get to their file.
My question is how can i download just the files and not the entire folder structure. SO when they open the zip file its all of the files and they don't have to go through 3 or 4 folder directories.
If you want to flatten the paths in your ZIP file, use this.
zip.AddFiles(manypaths, #"\");
The second parameter of AddFiles allows you to specify the path of your files in the archive and \ is the root in your archive. Therefore, all files will be located directly in your archive without subfolders.
I want to create content of a zip file in treeview, but my problem is how to recognize the content of zip file is file or directory without extract the files and then add them to tree?
would you mind help me in solving this?
This functionality is available out of the box in .Net Framework 4.5 and later. You have to use this library:
using System.IO.Compression;
And then you'll be able to do this:
string zipPath = #"c:\example\start.zip";
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry.FullName.EndsWith('\'))
Console.WriteLine($"{entry.FullName} is a directory.");
else
Console.WriteLine($"{entry.FullName} is a file.");
}
}
See here for more details:
How to list the contents of a .zip folder in c#?
I have a directory Structure like this:
A.zip -
A -
a -
1.dat
2.dat
I want to read the files 1.dat and 2.dat inside the directory hierarchy. I am able to read the file contentby C#, if the file is directly inside the zip folder but due to the inner directory structure is become inaccessible.
Any Help would be appreciated.
Thanks in advance.
Not sure how you are reading your zip file contents without an example, however reading zip file contents using the System.IO.Compression and System.IO.Compression.FileSystem assemblies is pretty simplistic. See the following example of how to read all files regardless of subdirectory within a zip file:
using System;
using System.IO.Compression;
namespace ZipReader
{
class Program
{
const string zipPath = #"D:\test\test.zip";
static void Main(string[] args)
{
using (var archive = ZipFile.OpenRead(zipPath))
{
foreach (var entry in archive.Entries)
{
Console.WriteLine(entry.FullName);
}
}
Console.ReadKey();
}
}
}
Produces the following output:
folder1/test1.txt
folder2/test2.txt
To get the contents you can simply call entry.Open() on each file which returns a Stream you can handle however you need to.
I'm using SevenZIP library files to unzip/extract .exe file. When i tried this approcah I'm getting a error Cannot read that as a ZipFile & zip exception was unhanded. I don't want to use any 7zip.exe console app in my project & i prefer to use .dll files in my project.
Is there any other way to extract .exe file?
private void MyExtract()
{
if(x86)
ExtractZip(#"D:\22.1.2.702\64\953-win_x86.exe", ".");
else
ExtractZip(#"D:\22.1.2.702\64\.702-win_x64.exe", ".");
}
private void ExtractZip(string zipFile, string directory)
{
using (var zip1 = ZipFile.Read(zipFile))
{
// here, we extract every entry, but we could extract conditionally
// based on entry name, size, date, checkbox status, etc.
foreach (var e in zip1)
{
e.Extract(directory, ExtractExistingFileAction.OverwriteSilently);
}
}
}
Code sample looks like you are using DotNetZip and not SevenZipLib. DotNetZip can only extract .zip files, not 7-zip nor .exe.
Instead of using SevenZip lib, try 7zip.exe in console. Use Process class to execute 7zip.exe. It works perfect.
What do I need to compress to .zip (not .gzip) in C# and how do I go about it?
I just need a quick answer, more preferably a link?
Thanks
DotNetZip is a good option. (http://dotnetzip.codeplex.com/)
It is pretty easy and quick.
Here is an example from the site:
Zip:
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");
}
Extract:
using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
zip.ExtractAll(TargetDirectory);
}
http://sharpdevelop.net/OpenSource/SharpZipLib/