Compressing Files With C# By Winrar - c#

I never done this but I am develping a simple application and really need some help.
The scenario is that I have some excel files which are all name coded, I need to get the first 5 letters of each files and compress those together. Ex.
MARKS
MARKS 2
MARKS 3
These all compress.
Is their any method to do this and zip with winrar.
Thanks

I agree IonicZip (now dotnetzip) is good. here is the sample of what you want to do
string somepath = "D:\\ExcelFiles";
string zippath = "D:\\ExcelFiles\\some.zip";
string[] filenames =
System.IO.Directory.GetFiles(somepath, "Mark*.xlsx", SearchOption.AllDirectories);
using (ZipFile zip = new ZipFile())
{
foreach (String filename in filenames)
{
ZipEntry e = zip.AddFile(filename, "");
}
zip.Save(zippath);
}

I use Ionic Zip in my project. API call is very easy. You can simply zip your files.
using (ZipFile zip = new ZipFile())
{
// add this map file into the "images" directory in the zip archive
zip.AddFile(#"C:\Users\kth\Desktop\dll\MARKS.xl", "images");
zip.Save("MyZipFile.zip");
}

Related

DotNetZip Creates Subfolders Containing the File Paths [duplicate]

This question already has answers here:
DotNetZip add files without creating folders
(4 answers)
Closed 2 years ago.
I .zip a file using DotNetZip, but inside contains subfolders of the actual filepath.
Example: Open Zip > (Users) folder > (Admin) folder > (Desktop) folder > file1.csv
May I know where I should change to that the .zip only contains the file itself?
using (ZipFile zip = new ZipFile())
{
zip.Password = "password";
zip.AddFile("C:\\Users\\Admin\\Desktop\\File1.csv");
zip.Save("Encrypted_File1.zip");
}
I am unsure how to change the .AddFile statement as there is no declaration of file path anywhere else.
Based on the documentation, I believe you need to write it like this:
using (ZipFile zip = new ZipFile())
{
zip.Password = "password";
zip.AddFile("C:\\Users\\Admin\\Desktop\\File1.csv", "Admin\\Desktop\\File1.csv");
zip.Save("Encrypted_File1.zip");
}
P.S. If you're doing many files and you have a common base path, you could use something like Path.GetRelativePath to get the in-zip path:
using (ZipFile zip = new ZipFile())
{
string commonBasePath = "C:\\Users";
// for each file
string filePath = "C:\\Users\\Admin\\Desktop\\File1.csv";
string inZipPath = Path.GetRelativePath(commonBasePath, filePath);
zip.Password = "password";
zip.AddFile(filePath, inZipPath);
// done
// save
zip.Save("Encrypted_File1.zip");
}

How to Extract ZIP file from specific path inside ZIP C# Ionic

I have one ZIP file named abc.ZIP
Inside ZIP folder structure is as below:
--abc
---pqr
----a
----b
----c
I want to extract this ZIP at D:/folder_name
But i want to extract only folder and its content named a,b,c. Also folder names are not fixed. I dont want to extract root folder abc and its child folder pqr.
I used following code but its not working:
using (ZipFile zipFile = ZipFile.Read(#"temp.zip"))
{
foreach (ZipEntry entry in zipFile.Entries)
{
entry.Extract(#"D:/folder_name");
}
}
The following should work, but I'm not sure, if it's the best option.
string rootPath = "abc/pqr/";
using (ZipFile zipFile = ZipFile.Read(#"abc.zip"))
{
foreach (ZipEntry entry in zipFile.Entries)
{
if (entry.FileName.StartsWith(rootPath) && entry.FileName.Length > rootPath.Length)
{
string path = Path.Combine(#"D:/folder_name", entry.FileName.Substring(rootPath.Length));
if (entry.IsDirectory)
{
Directory.CreateDirectory(path);
}
else
{
using (FileStream stream = new FileStream(path, FileMode.Create))
entry.Extract(stream);
}
}
}
}
Other option would be to extract the complete file in a temporary directory and move the sub directories to your target directory.

Create zip file from all files in folder

I'm trying to create a zip file from all files in a folder, but can't find any related snippet online. I'm trying to do something like this:
DirectoryInfo dir = new DirectoryInfo("somedir path");
ZipFile zip = new ZipFile();
zip.AddFiles(dir.getfiles());
zip.SaveTo("some other path");
Any help is very much appreciated.
edit: I only want to zip the files from a folder, not it's subfolders.
Referencing System.IO.Compression and System.IO.Compression.FileSystem in your Project
using System.IO.Compression;
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);
To use files only, use:
//Creates a new, blank zip file to work with - the file will be
//finalized when the using statement completes
using (ZipArchive newFile = ZipFile.Open(zipName, ZipArchiveMode.Create))
{
foreach (string file in Directory.GetFiles(myPath))
{
newFile.CreateEntryFromFile(file, System.IO.Path.GetFileName(file));
}
}
Referencing System.IO.Compression and System.IO.Compression.FileSystem in your Project, your code can be something like:
string startPath = #"some path";
string zipPath = #"some other path";
var files = Directory.GetFiles(startPath);
using (FileStream zipToOpen = new FileStream(zipPath, FileMode.Open))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
{
foreach (var file in files)
{
archive.CreateEntryFromFile(file, file);
}
}
}
In some folders though you may have problems with permissions.
To make your zipfile portable on UNIX system, you should pay attention to:
Compression.ZipFile support for Unix Permissions
For instance, one may use mod "644":
var entry = newFile.CreateEntryFromFile(file, file);
entry.ExternalAttributes |= (Convert.ToInt32("644", 8) << 16);
This does not need loops. For VS2019 + .NET FW 4.7+ did this...
Find ZipFile in Manage Nuget Packages browse, or use
https://www.nuget.org/packages/40-System.IO.Compression.FileSystem/
Then use:
using System.IO.Compression;
As an example, below code fragment will pack and unpack a directory (use false to avoid packing subdirs)
string zippedPath = "c:\\mydir"; // folder to add
string zipFileName = "c:\\temp\\therecipes.zip"; // zipfile to create
string unzipPath = "c:\\unpackedmydir"; // URL for ZIP file unpack
ZipFile.CreateFromDirectory(zippedPath, zipFileName, CompressionLevel.Fastest, true);
ZipFile.ExtractToDirectory(zipFileName, unzipPath);

Ionic Zip cannot zip folder inside folder C#

I am trying to zip a folder using IonicZip library in C#. It is able to zip .txt, .exe, .pdf etc. in the folder, but not able to zip folder in folder.
It is able to zip MusicLogs.txt and Video.exe but cannot Music folder.Music folder contains some folder also. It doesnt see Music folder even while debugging. My code is :
string zipPath = #"C:\" + DateTime.Now.ToString("yyyy-MM-dd HH-mm") + ".zip"; // zipped file extracts here
string filename = #"E:\"; // the fodler which should be zipped. File must be exist
using (ZipFile zipFile = new ZipFile())
{
zipFile.Password = "asd";
zipFile.Encryption = EncryptionAlgorithm.PkzipWeak;
foreach (string file in Directory.GetFiles(filename)) // this foreach is for getting all files in a folder.
{
zipFile.AddFile(file, "YESMusic"); // set file
}
zipFile.Save(zipPath);
}
Where is the problem ? Need a change in AddFile function ? Thanks
This will work:
using (ZipFile zipFile = new ZipFile())
{
zipFile.Password = "asd";
zipFile.Encryption = EncryptionAlgorithm.PkzipWeak;
// Adding folders in the base directory
foreach (var item in Directory.GetDirectories(filename))
{
string folderName = new DirectoryInfo(item).Name;
zipFile.AddDirectory(item, folderName);
}
// Adding files in the base directory
foreach (string file in Directory.GetFiles(filename))
{
zipFile.AddFile(file);
}
zipFile.Save(zipPath);
}

zip does'n show me any issue

Here i have several images and i need to zip those images and need to download it..In here i use Ionzip.The problem is that zip is not working.It doen't shows me any error.
MyCode
public bool DownloadImgs()
{
string Path = HttpContext.Current.Server.MapPath("../Content/images/QImages");
string zippath = HttpContext.Current.Server.MapPath("../Content/images/QImages/zipped/");
string[] filenames = System.IO.Directory.GetFiles(Path, "*.jpg", SearchOption.AllDirectories);//It returns all the paths of the images.
using (ZipFile zip = new ZipFile())
{
foreach (String filename in filenames)
{
ZipEntry e = zip.AddFile(filename, "");
}
zip.Save(zippath);//In here i need to download the zipped file.not to save
}
}
PS: This application is built using MVC framework
You should write the resulting zip to the Response stream.
From a MVC controller:
return this.File(zippath, "application/zip");
From an ASP.NET handler or page:
Response.TransmitFile(zippath);
Another option is to directly save the zip file to the response stream, which will optimize your disk usage.
You can save your zip file straight to the response outputstream:
zipFile.Save(Response.OutputStream);

Categories

Resources