DotNetZip How Add Selected Files without creating folders - c#

I want to add in zip file "test" all pdf files from path
using (var zip = new ZipFile())
{
zip.AddSelectedFiles("*.pdf",path);
zip.Save(path+"/test.zip");
}
when test.zip file is created have this directory :
**test.zip**\Users\administrator\Documents\vs2010\Projects\my project\**pdf files**
How to make all pdf documents to be directly in test.zip
test.zip\pdf files

Please try the following,
using (ZipFile zip = new ZipFile())
{
string[] files = Directory.GetFiles(path);
// filter the files for *.pdf
zip.AddFiles(files, "Test"); //Test Folder
zip.Save(path+"/test.zip");
}

Related

Can't set password using DotNetZip

I'm trying to password protect a zip file using DotNetZip. But it doesn't work. It creates the zip just fine, but if i open it using 7zip I can extract the files without a password. Here is the code I'm using.
using (ZipFile zip = new ZipFile())
{
zip.Password = password;
zip.Encryption = EncryptionAlgorithm.WinZipAes256;
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestSpeed;
// Adding folders in the base directory
foreach (var item in Directory.GetDirectories(someFilePath))
{
string folderName = new DirectoryInfo(item).Name;
zip.AddDirectory(item, folderName);
}
// Adding files in the base directory
foreach (string file in Directory.GetFiles(someFilePath))
{
zip.AddFile(file, "");
}
zip.Save(someFilePath);
}
Okay, I fixed it. I downloaded an older version of the DotNetZip dll.
I was previously using version 1.12 and it didn't work.
Using version 1.10.1 and the created zip is password protected

How to add a folder to a folder when creating a zip file in asp using Ionic.Zip

I am able to successfully create a .zip file using AddDirectoryByName and AddFile , my code below :-
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
zip.AddDirectoryByName("A");
zip.AddFile("~/.png", "A");
}
but what happens is that, it creates a folder by name A
and inside it, it adds a file (eg. .png).
But I want to place this folder A inside another created folder called "Root", so now how can I create a folder called Root to my .zip and add folder A to that Root ??
Any help thankfully appreciated.
Simply use the full path name when creating a new directory.
using(ZipFile zip = new ZipFile())
{
string directoryA = "Root/A";
string directoryB = "Root/B";
zip.AddEntry($"{directoryA}/readmeA.txt", "Success from Directory A");
zip.AddEntry($"{directoryB}/readmeB.txt", "Success from Directory B");
zip.Save("file.zip");
}

Zip files in a folder using c# and return response

I want to zip files and folders in a folder using c# and I've checked previously asked questions but they don't work for me...i'm currently trying to work with DotNetZip. Here's a bit of my code:
using (ZipFile zip = new ZipFile())
{
string[] files = Directory.GetFiles(#"C:\Users\T1132\Desktop\logs");
// add all those files to the logs folder in the zip file
zip.AddFiles(files, "logs");
zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
var a = System.DateTime.Now.ToString();
zip.Save(#"C:\Users\T1132\Desktop\logs\Archiver.zip");
foreach (var f in files)
{
System.IO.File.Delete(f);
System.Diagnostics.Debug.WriteLine(f + "will be deleted");
}
}
the code above works but it only zips the files and leaves the folders. Kindly assist me please, Thanks.
There are so many ways to zip files using DotNetZip.
using (ZipFile zip = new ZipFile())
{
zip.Encoding = System.Text.Encoding.GetEncoding("big5"); // chinese
zip.AddDirectory(#"MyDocuments\ProjectX");
zip.Save(ZipFileToCreate);
}
Above zips a directory. For more uses you can follow the links
https://dotnetzip.codeplex.com/wikipage?title=CS-Examples&referringTitle=Examples
or
http://grapevine.dyndns-ip.com/download/authentec/download/dotnetzip/examples.htm
Thanks
I am using this and it is working great. Make sure directory has enough permission(s).
System.IO.Compression.ZipFile.CreateFromDirectory(zipPath, path + strFileName + ".zip");
You can use below code.
string zipFileName= "zip full path with extension";
var files = Directory.GetFiles(" directory path");
Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile();
foreach(var file in files) {
zip.AddFile(file);
}
zip.Save(zipFileName);

C# -Make a zip file from List of file paths

I have a number of folders and I have number of file in each folders.
I am taking some of the file paths from different folders and I need to zip those files into a single zip file.
if I have the file in a single folder, I can do zip using
string startPath = #"c:\example\start";//folder to add
string zipPath = #"c:\example\result.zip";//URL for your ZIP file
ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest, true);
string extractPath = #"c:\example\extract";//path to extract
ZipFile.ExtractToDirectory(zipPath, extractPath);
using (ZipArchive newFile = ZipFile.Open(zipName, ZipArchiveMode.Create))
{
foreach (string file in Directory.GetFiles(myPath))
{
newFile.CreateEntryFromFile(file);
}
}
Suppose I don't know about the folders, I only have a list of file paths, I need to foreach through each file path and add into the zip. What should I do for this?
You can enumerate all files in the directory include subdirectories with Directory.GetFiles overloading and SearchOption.AllDirectories option and then use it
String[] allfiles = System.IO.Directory.GetFiles("myPath", "*.*", System.IO.SearchOption.AllDirectories);
foreach (string file in allfiles)
{
newFile.CreateEntryFromFile(file);
}
You can use ZipFile.CreateFromDirectory(string sourceDirectoryName, string destinationArchiveFileName) or its overload to create an archive in one step too.
Also there is a way to manipulate zip archive structure more flexibly via Path.GetDirectoryName() method. Here is the example:
foreach(var filePath in files)
{
var directory = Path.GetDirectoryName(filePath);
var fileName = Path.GetFileName(filePath);
var entry = archive.GetEntryFromFile(filePath, $"{directory}/{fileName}");
}
Finally, you can use 3rd party library DotNetZip and solve yor scenario in just 3 lines of code:
using (ZipFile zip = new ZipFile())
{
zip.AddFiles(files);
zip.Save("Archive.zip");
}
Using the DotNetZip library from newget manager, just check whether it will work or not
using (ZipFile zip = new ZipFile())
{
foreach(var filestring in AllFileArray)
{
zip.AddFile(filestring);
}
zip.save("MyZipFile.zip")
}

Is a way to use System.IO.Compression.ZipFile in C# to add folder without createFromDirectory?

I need to zip a folder that contains folders, common files and zipfiles. But I need to except the files with the "zip" extension. Also I need to create the zipfile or update it if is already created.
In order to do this, I check all files and directories and then add the files to the zip file. So far so good, but I can't find a way to add or update a folder.
This is my last tried:
var files = Directory.GetFiles(directoryToCompress).Where(name => !name.EndsWith(".zip", true, System.Globalization.CultureInfo.CurrentCulture));
var directories = Directory.GetDirectories(directoryToCompress);
if (File.Exists(filenameZip))
{
using (ZipArchive zipDest = ZipFile.Open(filenameZip,ZipArchiveMode.Update))
{
foreach (var file in files)
{
zipDest.
zipDest.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
System.IO.File.Delete(file);
}
}
}
else
{
using (ZipArchive zip = ZipFile.Open(filenameZip, ZipArchiveMode.Create))
{
foreach (var file in files)
{
zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
System.IO.File.Delete(file);
}
}
}
Thanks in advance for any tip or help

Categories