Click here to Skip to main content
13,196,566 members (54,521 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 14 Jan 2008

How to downlaod an image file as a Zip file using C#.NET and J#

, 11 May 2008
Rate this:
Please Sign up or sign in to vote.
Code to help downlaod an image file as a Zip file using C#.NET and J#.


Recently, I had to build a club website where the user has the ability to download an image as a zip file. So, I planned to make a procedure which will automatically convert an image file to a zip file. Online search led me to a blog with this wonderful idea of using the ZIP option in J#, and it really attracted me.


In this article, I will explain the usage of ZIP functionality in J# from C# code. The code in this application has been designed to reuse in a copy-paste fashion and not as a library.

This application consumes J# classes internally. For this, we must first refer to the J# .NET library. Physically, it resides as a file named vjslib.dll. If you are not very sure how to refer to a library in your project, please follow the steps below:

Right click your project in Server Explorer and click on "Add Reference" -> select the .NET tab -> scroll down and select "vjslib" -> click OK and you are there. Now, you can refer the Java library classes within your application.

Listing 1 - Directives
using java.util;

The namespace contains the classes and methods to implement the compress and uncompress functionalities. The main classes used from the above namespaces are

  • ZipFile
  • ZipEntry
  • ZipOutputSteam
  • Enumeration
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using java.util;
public partial class _Default : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e)
    protected void lnkImage_Click(object sender, EventArgs e)
       string FilePath = Server.MapPath("~/Image/");
       string FileDest = Server.MapPath("~/Image/");
       Zip(FileDest + "", FilePath + "pic.jpg");
    private void Zip(string zipFileName, string sourceFile)
        FileOutputStream filOpStrm = new FileOutputStream(zipFileName);
        ZipOutputStream zipOpStrm = new ZipOutputStream(filOpStrm);
        FileInputStream filIpStrm = null;
        filIpStrm = new FileInputStream(sourceFile);
        ZipEntry ze = new ZipEntry(Path.GetFileName(sourceFile));
        sbyte[] buffer = new sbyte[1024];
        int len = 0;
        while ((len = >= 0)
            zipOpStrm.write(buffer, 0, len);

        FileInfo file = new FileInfo(zipFileName); 
        Response.ContentType = "application/octet-stream";
          "attachment; filename=" + "");
    protected void lnkSource_Click(object sender, EventArgs e)
        FileInfo file = new FileInfo(Server.MapPath("~/Image/") + 
        Response.ContentType = "application/octet-stream";
          "attachment; filename=" + "");


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Souvik Das, MCP, ITIL
Software Developer (Senior) OHI Group
Oman Oman
I am Souvik Das, Microsoft Certified Professional, ITIL, M.C.A post graduate having over 7 years of experience as a Sr. Software Developer. I'm currently working as a Sr. Software Developer from OHI Group.

You may also be interested in...


Comments and Discussions

GeneralExcellent Pin
swadhinm8-Nov-11 1:23
memberswadhinm8-Nov-11 1:23 
GeneralLittle spelling correction Pin
ricoohh29-Mar-09 23:16
memberricoohh29-Mar-09 23:16 
GeneralRuntime Pin
RichardGrimmer13-May-08 1:06
memberRichardGrimmer13-May-08 1:06 
QuestionWhat about the GZipStream Pin
Doubin11-May-08 21:55
memberDoubin11-May-08 21:55 
It comes with c# 2.0 not need to hook up J#.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171019.1 | Last Updated 12 May 2008
Article Copyright 2008 by Souvik Das, MCP, ITIL
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid