.NET Answers

ASP.NET, HTML, CSS, Visual Studio, CSharp, VB.NET and other programming items of interest.
Subscribe
  • Home
  • About Me
  • Advertising
  • Click Here to Ask a question
    • Privacy Policy
  • Site Map

Did you know – Zip is built into .NET?

August 21, 2008 By: Dave

food-cof-01 A couple of weeks ago, I was working on a project that required me to unzip a file.  There is only one text file in the zip, so using a full-blown library like SharpZipLib (OpenSource) or some commercial product just didn’t seem right.  From my experience with Java, I knew zip routines were available as part of the Java libraries in .NET (JSharp).  It seemed like my best bet would be to use what was already available rather than having yet another DLL on my system.

The two classes you will need from the JSharp libray are ZipFile and ZipEntry.  These are both located in the java.util.zip namespace.  You will need to add a reference to the vjslib assembly too.

To load the file from your drive, use:

ZipFile zf = new ZipFile(FileName);

To get the entires out of the zip file, use:

ZipEntry ze = (ZipEntry)(zf.entries().nextElement());

If you had multiple entries in your zip file, you’d need to put that in a loop of course.  But since my zip file only has one entry, that is all the code I need to get that entry.

The rest of the code is simply pulling the data out of the stream.

InputStreamProxy isp =
    new InputStreamProxy(zf.getInputStream(ze));

We will take a look at the InputStreamProxy class later on, but for now all you need to know is that it converts the Java InputStream that getInputStream() returns into a .NET InputStream so that the rest of our code can use it.  This is the one place where I think Microsoft could have helped us out a bit.  Every Java to .NET port involving streams has this code or something very similar in it.

byte[] content = new byte[1024];
StringBuilder sb = new StringBuilder();
int cnt = 0;
while ((cnt = isp.Read(content, 0, 1024)) > 0)
{

    string str;
    System.Text.ASCIIEncoding enc =
        new System.Text.ASCIIEncoding();
    str = enc.GetString(content);
    str = str.Substring(0, cnt);
    sb.Append(str);
}

This is just your basic “read bytes from a stream” code.  Since we want to use the data as plain text, we need to use the ASCIIEncoding() method to convert the bytes to a string.  You’ll notice that I’m also using a StringBuilder to build up my string efficiently.

So the only thing left to show is the InputStreamProxy class.  There really isn’t a whole lot to show since all it does is wrap the java.io.InputStream class that the getInputStream method returns.

class InputStreamProxy : Stream
{
    private java.io.InputStream istrm;

    public InputStreamProxy(java.io.InputStream istrm)
    {
        this.istrm = istrm;
    }

    /** @property */
    public java.io.InputStream get_JavaStream()
    {
        return istrm;
    }

    /** @property */
    public override bool CanRead
    {
        get
        {
            return true;
        }
    }

    /** @property */
    public override bool CanSeek
    {
        get
        {
            return false;
        }
    }

    /** @property */
    public override bool CanWrite
    {
        get
        {
            return false;
        }
    }

    /** @property */
    public override long Length
    {
        get
        {
            throw new System.NotSupportedException();
        }
    }

    /** @property */
    public override long Position
    {
        get
        {
            throw new System.NotSupportedException();
        }
        set
        {
            throw new System.NotSupportedException();
        }
    }

    public override void Flush()
    {
        //do nothing
    }

    public override int Read(byte[] buffer,
        int offset, int count)
    {

        int readBytes;
        try
        {
            readBytes =
                istrm.read((sbyte[])((object)buffer),
                offset, count);
        }
        catch (java.io.IOException ioe)
        {
            throw new System.IO.IOException(ioe.Message);
        }
        return readBytes;
    }

    public override long Seek(long offset,
        SeekOrigin origin)
    {
        throw new System.NotSupportedException();
    }

    public override void SetLength(long value)
    {
        throw new System.NotSupportedException();
    }

    public override void Write(byte[] buffer,
        int offset, int count)
    {
        throw new System.NotSupportedException();
    }

    public override void Close()
    {
        try
        {
            istrm.close();
        }
        catch (java.io.IOException ioe)
        {
            throw new
                System.ObjectDisposedException(
                    ioe.getMessage());
        }
    }
}

You’ll see that the bulk of the code is in the Read method which simply calls the Read method of the Java stream.

 

Other post in Did you know
  • Did you know - Zip is built into .NET? - August 21st, 2008
  • Panel DefaultButton does not work - September 23rd, 2008
  • Automatic Implementation of Interfaces - October 1st, 2008
  • Easily Find Classes, Methods, and Variables - November 17th, 2008
  • Advantages of Using Class Diagram - November 20th, 2008
  • Renaming Properties, Methods and Variables - November 25th, 2008
  • Finding a CSS Class Definition - December 3rd, 2008

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Related Post

  • .Net String Pool – Not Just For The Compiler
  • Forcing Extension Methods to Be Used
  • SQL For Programmers – CREATE TABLE
Bookmark to:

Add to Del.icio.us Add to digg Add to DotNetKicks Add to DZone Add to Facebook Add to Slashdot Add to Stumble Upon Add to Technorati
Hide Sites
Tags: .net, compression, csharp, java, zip

One Response to “ Did you know – Zip is built into .NET? ”

  1. # 1 James Says:
    November 25th, 2008 at 12:53 pm

    Actually, it’s not built into the .NET Framework. It’s built into the J# Runtime Package… and J# is a separate install that runs on top of .NET 2.0 or newer.

    So you have yet-another-DLL, except in this case, you have the baggage of the full J# runtime instead of just a small DLL… and you have the added hassle of deploying J# with your app rather than just dropping in one more DLL into your app’s existing deployment.

← SQL for Programmers – CREATE/DROP Database
SQL For Programmers – CREATE TABLE →
  • Search

  • Subscribe

    U COMMENT
    I FOLLOW

    Subscribe in a reader

    OR

    Subscribe via e-mail

    Enter your email address: 

    Delivered by FeedBurner

     

  • Follow Me

    • Twitter
    • FaceBook
    • Digg
    • StumbleUpon
    • Propeller
    • Delicious
    • Plaxo

     

  • Recent Posts

    • ASUS Eee PC 1005HA-PU1X-BK Black Netbook
    • jQuery – Date Picker
    • Using VB.NET From CSharp
    • iTextSharp – Adding Images
    • Hungarian Notation – Use What Works, Spit Out The Bones
    • Pre Order Windows 7
    • jQuery Dialog – With Validation Controls
    • iTextSharp – The easy way
    • Structure of my ASP.NET Web Applications
    • 35% Off Accronis True Image 2009 Home
    • VB.NET Hide Module Name
    • ASP.NET/VB.NET – Video Training
    • Does jQuery Make Us Lazy?
    • PDFs Using iTextSharp
    • Programming SEO – Ping



  • Advertise on this site through Lake Quincy Media
  • DotNetNuke Sponsor

     

    Most Valuable Blogger
  • Sponsor

  • Categories

    • Advanced CSharp
    • Advanced VB.NET
    • ASP.NET MVC
    • Did you know
    • DotNetNuke – Module Development
    • DotNetNuke – Skinning
    • internationalization
    • iTextSharp
    • jQuery
    • none
    • Seach Engine Optimization
    • Silverlight
    • SQL For Programmers
    • Twitter
    • winforms
  • Cloud

    .net ajax architecture asp.net book books containers csharp css dal dataset datasets dotnetnuke events gridview images internationalization internet explorer javascript jQuery json linq listview modules ms-sql MVC objectdatasource programming reflection seo Silverlight skinning sql testing tsql tutorial Twitter twitterizer vb.net video view Vista visual studio webservice WordPress
  • Archives

    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
    • Privacy Policy
  • Calendar

    August 2008
    S M T W T F S
    « Jul   Sep »
     12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31  
  • Blogroll

    • Alvin Ashcraft’s Morning Dew
    • ASP.NET Consulting
    • Life Hacker
    • Remember Anything
    • The Price of Their Toys
    • Uncategorized Thought


.NET Answers © 2007 - 2008 All Rights Reserved.
Entries and Comments.