Wednesday, July 24, 2013

Compress the Large XML Files using 7zip C# API

Hi All, Compressing the files really helps if the file size is Huge (>1 GB) and compression ration is high. 7zip C# code API helps to achieve the target

You need 2 external files to create a project/solution to zip files using this SevenZip C# Code SevenZipSharp.dll ,
get this from http://sevenzipsharp.codeplex.com/ the above file is a .net assembly and cab be directly referenced in C# code 7-zip.dll, get it from http://www.7-zip.org/ 

This is not a .net assembly but a COM Use the below code to build the solution, this code is compressing around 6.5 GB files to 1 MB in 2 minutes.



Code:
class Program { static void Main(string[] args) { Console.WriteLine("Enter the 7zip dll location :"); string zipfilenametozip = Console.ReadLine(); SevenZip.SevenZipBase.SetLibraryPath(zipfilenametozip); MemoryStream ms = new MemoryStream(); MemoryStream compressedStream = new MemoryStream(); SevenZipSevenZipCompressor compressor = new SevenZipSevenZipCompressor(); compressor.CompressionMethod = SevenZip.CompressionMethod.Lzma2; compressor.CompressionLevel = SevenZip.CompressionLevel.Low; compressor.CompressStream(ms, compressedStream); compressedStream.Position = 0; Console.WriteLine("Enter the uncompressed File Name with location to Compress (Zip) :"); string filenametozip = Console.ReadLine(); Console.WriteLine("Enter the File Name with location for the Compressed zip File :"); string filenamezipped = Console.ReadLine(); Console.WriteLine("Zipping started for file Name : " + filenametozip + " at " + DateTime.Now.ToString()); string[] myfiles = { filenametozip }; compressor.CompressFiles(filenamezipped, myfiles); Console.WriteLine("Zipping completed : " + filenamezipped + " at " + DateTime.Now.ToString()); } } 



Please comment if you face any issues in using this code... Thanks, Prajesh Jha

No comments: