I'm trying to make a method of compressing file using Ionic.Zip.dll
of DotNetZip.
Code for Compression:
using Ionic.Zip;
public string Compress(string[] Paths, string SaveFileName, string Password, string CompressionType)
{
try
{
using (ZipFile zip = new ZipFile())
{
if (Password != string.Empty)
zip.Password = Password;
zip.CompressionLevel = Utility.GetCompressionLevel(CompressionType);
foreach (string item in Paths)
{
if (IsDirectory(item))
zip.AddDirectory(item);
else if (IsFile(item))
zip.AddFile(item);
}
zip.Save(SaveFileName);
}
return ExplorerResource.ResourceManager.GetString("ZipSuccess");
}
catch (Exception ex)
{
return ex.Message;
}
}
Custom class:
public class Utility
{
public static Ionic.Zlib.CompressionLevel GetCompressionLevel(string Expression)
{
Ionic.Zlib.CompressionLevel result = Ionic.Zlib.CompressionLevel.None;
if (Ionic.Zlib.CompressionLevel.BestCompression.ToString().ToUpper() == Expression.Trim().ToUpper())
result = Ionic.Zlib.CompressionLevel.BestCompression;
if (Ionic.Zlib.CompressionLevel.BestSpeed.ToString().ToUpper() == Expression.Trim().ToUpper())
result = Ionic.Zlib.CompressionLevel.BestSpeed;
if (Ionic.Zlib.CompressionLevel.Default.ToString().ToUpper() == Expression.Trim().ToUpper())
result = Ionic.Zlib.CompressionLevel.Default;
if (Ionic.Zlib.CompressionLevel.Level0.ToString().ToUpper() == Expression.Trim().ToUpper())
result = Ionic.Zlib.CompressionLevel.Level0;
if (Ionic.Zlib.CompressionLevel.Level1.ToString().ToUpper() == Expression.Trim().ToUpper())
result = Ionic.Zlib.CompressionLevel.Level1;
if (Ionic.Zlib.CompressionLevel.Level2.ToString().ToUpper() == Expression.Trim().ToUpper())
result = Ionic.Zlib.CompressionLevel.Level2;
if (Ionic.Zlib.CompressionLevel.Level3.ToString().ToUpper() == Expression.Trim().ToUpper())
result = Ionic.Zlib.CompressionLevel.Level3;
if (Ionic.Zlib.CompressionLevel.Level4.ToString().ToUpper() == Expression.Trim().ToUpper())
result = Ionic.Zlib.CompressionLevel.Level4;
if (Ionic.Zlib.CompressionLevel.Level5.ToString().ToUpper() == Expression.Trim().ToUpper())
result = Ionic.Zlib.CompressionLevel.Level5;
if (Ionic.Zlib.CompressionLevel.Level6.ToString().ToUpper() == Expression.Trim().ToUpper())
result = Ionic.Zlib.CompressionLevel.Level6;
if (Ionic.Zlib.CompressionLevel.Level7.ToString().ToUpper() == Expression.Trim().ToUpper())
result = Ionic.Zlib.CompressionLevel.Level7;
if (Ionic.Zlib.CompressionLevel.Level8.ToString().ToUpper() == Expression.Trim().ToUpper())
result = Ionic.Zlib.CompressionLevel.Level8;
if (Ionic.Zlib.CompressionLevel.Level9.ToString().ToUpper() == Expression.Trim().ToUpper())
result = Ionic.Zlib.CompressionLevel.Level9;
return result;
}
GUI of ZIP program:
But I'm getting this error when I start compressing the file.
I think the code is not compatible with .net version?
I hope someone could make a solution for this, or if you have a better way of doing the method please tell me.. Thanks in advance!
You need to download at least version 1.7 of dotnetzip and then use the compact framework-specific version (i.e. Ionic.Zip.CF.dll).