Sunday, January 28, 2007

Zip Online

I was just briefly playing around (again) with ICsharpCode's SharpZipLib and I made this little tool that I figured might come in handy some day.

It's an online zip opener. It's handy in the cases when you are asked to download a zip file, but you really don't feel like filling up your desktop with yet another zip when all you need is a single file for the content. Or perhaps for the cases where you'd like to link directly to a file within a zip. Or for when you want to index a zip file (as always I think in search) :-)

It's basically a web app, that takes two parameters: "zip" with a url to the zip file you want to examine and "file" for the file name/path within that zipfile that you want to extract.
In case you only give the "zip" parameter it will list the content of the zip file.

The most important method is this (and yes, I realize the code isn't pretty - it's just a simple poc):



protected void LoadZip(string url, string file)
{
Error.Text = "";
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
if (resp.ContentType == "application/x-zip-compressed")
{
if (resp.ContentLength < 2000000)
{
BinaryReader br = new BinaryReader(resp.GetResponseStream());
byte[] buf = br.ReadBytes((int)resp.ContentLength);
br.Close();

System.IO.MemoryStream ms = new System.IO.MemoryStream(buf);
ZipFile zf = new ICSharpCode.SharpZipLib.Zip.ZipFile(ms);

if (file != null)
{
ZipEntry ze = zf.GetEntry(file);
Stream filestream = zf.GetInputStream(ze);
BinaryReader br2 = new BinaryReader(filestream);
byte[] newbuf = br2.ReadBytes((int)ze.Size);
br2.Close();
Response.ClearContent();
Response.ContentType = MimeType(file);
BinaryWriter bw2 = new BinaryWriter(Response.OutputStream);
bw2.Write(newbuf);
bw2.Close();
Response.End();

}
else
{
GridView1.DataSource = zf;
GridView1.DataBind();
}
zf.Close();
}
else
{
Error.Text = "Zip file too big";
resp.Close();
}
}
else
{
Error.Text = "Wrong content type: " + resp.ContentType;
resp.Close();
}
}

A couple of parts was a bit tricky. First the part that loads the external zip into a memorystream for easy access by the sharpziplib. It turns out that the BinaryReader was quite necessary in order to load it without corrupting the data. Also determining which mimetype a given document in the zip file is, before sending it out wasn't all that easy, but in the end I found a solution online. This solution looks up mimetypes in the registry so it does require registry access for the aspnet user - that can potentially be a problem.
There's several known problems with this implementation, but I'll leave it to you guys to figure them out :-)

Download the code here.
Try it here (note that my "playground" server where this is on might be closed down pretty soon - enjoy while you can).

1 comment:

Alex said...

There are a lot of zip files on my PC. And one day something must have happened with my PC and all data were lost. I recovered most of lost data,but my xls files,where was my job files I couldn't. Today I by sheer chance observed a next tool - program to fix file compressed with winzip. I used it and was discouraged,by reason of it solved my issue for seconds and I didn't spend the money as far as I kept in mind. I hazard the remark that it helped my friend in like condition.