Tuesday, August 26, 2003

Ted Leung has a pointer to Chris Winters' comparison of Perl and Java. Ted wonders what the equivalents in other languages would be. .NET is a bit problematic, since the default FCL doesn't include a Zip library. That omission has always puzzled me. There's two solutions that I know of, the first is outlined in an MSDN article by Ianier Munoz. His solution is to use the Microsoft J# class libraries, which seems like a really strange approach, unless you already use J#. The other solution is to use SharpZipLib;;;. Using Chris' use case of "Most of the time all you want to do is unpack it to a directory, preserving the directory structure instead of flattening them out.", the C# code would look like this (mostly borrowed from a SharpZipLib sample):

 
using System; 
using System.IO; 
using ICSharpCode.SharpZipLib.Zip; 
class MainClass 
{ 
    public static void Main(string[] args) 
    { 
        ZipInputStream s = new ZipInputStream(File.OpenRead(args[0])); 
        ZipEntry theEntry; 
        while ((theEntry = s.GetNextEntry()) != null) 
        { 
            string directoryName = Path.GetDirectoryName(theEntry.Name); 
            string fileName = Path.GetFileName(theEntry.Name); 
            // create directory 
            if (directoryName != "") 
            { 
                Directory.CreateDirectory(directoryName); 
            } 
            if (fileName != String.Empty) 
            { 
                FileStream streamWriter = File.Create(theEntry.Name); 
                int size = 2048; 
                byte[] data = new byte[2048]; 
                while (true) 
                { 
                    size = s.Read(data, 0, data.Length); 
                    if (size > 0) 
                    { 
                        streamWriter.Write(data, 0, size); 
                    } 
                    else 
                    { 
                        break; 
                    } 
                } 
                streamWriter.Close(); 
            } 
        } 
        s.Close(); 
    } 
} 
Ultimately, not much simpler than Java.

In Python, this is simpler, though it took me a while to figure out what to do with directory entries in the zip file. There's probably a simpler way to do this, but here 'tis:

 
import zipfile 
get_filename_from_somewhere( ... ) 
z = zipfile.ZipFile(f,"t") 
for filename in z.namelist(): 
  p = os.path.abspath(os.path.dirname(filename)) 
  if not os.path.exists(p): 
    os.mkdir(p) 
  if os.path.basename(filename) <> '': 
    out = open(os.path.abspath(filename),"wb") 
    out.write(z.read(filename)) 
    out.close() 

I haven't been able to locate a general zip library for Lisp, though Franz has a deflate module available. Also, it looks like Ruby doesn't have standard zip format support, though I found at least one implementation. Looking at the samples, it looks like it would be mostly the same as Python, requiring you to handle each entry in the zip file separately.

Later: Dean Goodmanson tells me that there's another .NET zip implementation out there as well, though it seems to be less mature than SharpZipLib.

5:23:15 PM  permalink Click here to send an email to the editor of this weblog. 


Stories
DateTitle
1/23/2003 Why XML?
8/13/2002 Resolution for IE and Windows problems
8/10/2002 Supporting VS.NET and NAnt
5/11/2002 When do you stop unit testing?
Contact
jabber: weakliem@jabber.org
YM: gweakliem
MSN: gweakliem@pcisys.net
email: Click here to send an email to the editor of this weblog.
Subscribe to "Gordon Weakliem's Weblog" in Radio UserLand.
Click to see the XML version of this web page.