Click here to Skip to main content
Click here to Skip to main content

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

By , 11 May 2008
 

Introduction

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.

Background

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;
using java.util.zip;
using java.io;

The java.util.zip 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;
using java.util.zip;
using java.io;
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 + "test.zip", 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));
        zipOpStrm.putNextEntry(ze);
        sbyte[] buffer = new sbyte[1024];
        int len = 0;
        while ((len = filIpStrm.read(buffer)) >= 0)
        {
            zipOpStrm.write(buffer, 0, len);
        }

        zipOpStrm.closeEntry();
        filIpStrm.close();
        zipOpStrm.close();
        filOpStrm.close();
        FileInfo file = new FileInfo(zipFileName); 
        Response.ClearHeaders();
        Response.ClearContent();
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", 
          "attachment; filename=" + "test.zip");
        Response.WriteFile(file.FullName);
        Response.End();
    }
    protected void lnkSource_Click(object sender, EventArgs e)
    {
        //Downloadzip.zip
        FileInfo file = new FileInfo(Server.MapPath("~/Image/") + 
                                     "Downloadzip.zip");
        Response.ClearHeaders();
        Response.ClearContent();
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", 
          "attachment; filename=" + "Downloadzip.zip");
        Response.WriteFile(file.FullName);
        Response.End();
    }
}

License

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

About the Author

Souvik
Software Developer Databazaar India Pvt. Ltd
India India
Member
I am Souvik Das, M.C.A post graduate having over 4 years of experience as a Sr. Software Developer. I'm currently working as a Software Developer from Databazaar India Pvt. Ltd.
 
I am proficient in :ASP.NET, VB.NET, C#.NET

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralExcellentmemberswadhinm8 Nov '11 - 1:23 
Smile | :)
GeneralLittle spelling correctionmemberricoohh29 Mar '09 - 23:16 
Hi,
i don't want to be pedantic...
 
maybe you change downlaod to download in your topic
 
regards
rIQ
 
Problems with Windows? re-boot.
Problems with Linux? be-root!

GeneralRuntimememberRichardGrimmer13 May '08 - 1:06 
Just a note - in order to use this on client's site, you need to install the VSJSRuntime package from MS.
 
C# has already designed away most of the tedium of C++.

QuestionWhat about the GZipStreammemberDoubin11 May '08 - 21:55 
It comes with c# 2.0 not need to hook up J#.
 
http://doubin.blogspot.com/

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

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