Introduction
While you're developing C# applications, you may need to perform some specific or special operations, such as zipping files to download, or sending an e-mail with a zip file attached, or performing a backup. This article proposes a solution for you to zip files and/or folders, respecting the whole tree of a folder, without the need to buy third-party solutions.
Background
C# usually uses GZip by default, which is not exactly a zip file (it is different). So, looking up for a solution, you have two kinds of options:
- Use third-party solutions
- Do it by yourself
Although there are free third-party solutions, such as...
... you may find yourself wanting to do it yourself, without these libraries, using exclusively the .NET standard libraries. This article shows the way. If you attempt to find the necessary code only on C#, you will find yourself lost. C# has code just for GZip, which is not zip. After Googling (a lot!), I've been able to find a solution, which uses a mix of C# and J#. J# provides libraries specifically to zip (java.util.zip). But you may be confused about "how do I use J# on C#! Easy, just add a reference to the vjslib.dll Library (see picture below), and import the namespaces to perform the zip needed actions.
Using the Code
The solution is quite simple, however, it requires you to select the correct methods and specific parameters. In my sample, you have a simple Web Application with a button and a label to display the result of this process. When you press the button, a mix of C# and J# is called to zip a folder. When .NET compiles the project, it will assemble this mix as one single application. (Notice that you must have J# libraries installer on your Web server, otherwise you won't be able to have this working.)
using java.io;
using java.util.zip;
using System.IO;
using System.Text;
protected void btnZip_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
string ZipFileName = String.Format(@"C:\ZippedFolders\({0}).MyZip.zip",
DateTime.Now.ToString("yyyyMMdd"));
string theDirectory = @"C:\FolderToZip";
try
{
sb.Append(String.Format("Directory To Zip: {0}.<br/>", theDirectory));
sb.Append(String.Format("Zip file: {0}.<br/>", ZipFileName));
string[] allFiles = Directory.GetFiles(theDirectory, "*.*",
SearchOption.AllDirectories);
if (System.IO.File.Exists(ZipFileName))
{
System.IO.File.Delete(ZipFileName);
sb.Append(String.Format
("Deleted old Zip file: {0}.<br/>", ZipFileName));
}
FileOutputStream fos = new FileOutputStream(ZipFileName);
ZipOutputStream zos = new ZipOutputStream(fos);
zos.setLevel(9);
for (int i = 0; i < allFiles.Length; i++ )
{
string sourceFile = allFiles[i];
FileInputStream fis = new FileInputStream(sourceFile);
ZipEntry ze = new ZipEntry(sourceFile.Replace(theDirectory + @"\", ""));
zos.putNextEntry(ze);
sbyte[] buffer = new sbyte[1024];
int len;
while ((len = fis.read(buffer)) >= 0)
{
zos.write(buffer, 0, len);
}
fis.close();
}
zos.closeEntry();
zos.close();
fos.close();
sb.Append(String.Format("Folder {0} Zipped successfully to File {1}.<br/>",
theDirectory, ZipFileName));
}
catch (Exception eX)
{
sb.Append(String.Format("Error zipping folder {0}. Details: {1}.
Stack Trace: {2}.<br/>", theDirectory, eX.Message, eX.StackTrace));
}
lbReport.Text = sb.ToString();
}
The physical folder to zip:
The result of the Web Application after pressing the button:
And the zipped files on the zip folder:
Points of Interest
The most interesting point about this solution is that you can use both C# and J# libraries on the same code, compiling them together in order to complete a solution. C# for itself, with its standard libraries, does not provide zip methods. J# does, however, you are programming in C#. So, why not mix them together in the same solution?!?
Another interesting point is the fact that there are several solutions on the web, all of them being libraries that you can download, but you must pay for their license. This one uses .NET libraries exclusively, no third party solutions involved.
History
After receiving some comments, a new version was generated, resizing the images, adding comments to the code and explaining this solution.