Weekend Links

Print Friendly, PDF & Email

I’d planned to spend the weekend writing up a new programming article for the site but it got a little hi-jacked by another project I was working on. On Friday, I needed to figure out how to generate ZIP files from a website and just couldn’t put it down for the weekend. The situation is one where the site is generating multiple text files for download and I want the user to be able to download all the files as one ZIP file.

So, naturally I hit Google and started looking up code libraries, preferably free ones, and I was able to find a couple. Of the two I tried, I’m thinking I prefer DotNetZip which made it really simple to generate a ZIP file with the exact files I needed – like this simple:


using Ionic.Zip;

protected void ZipTest()
StreamWriter swFile;
ZipFile newZip = new ZipFile();
string zipFN;
string streamFN;
int fileNumber = 1;


for (int x = 1; x <= 3; x++)
{
streamFN = Server.MapPath("~/Output") + "\\" + fileNumber.ToString("0000") + ".txt";
swFile = new StreamWriter(streamFN);
swFile.WriteLine("File " + DateTime.Now.ToFileTime());
swFile.WriteLine("Created on " + DateTime.Now.ToString());
swFile.Flush();
swFile.Close();
newZip.AddFile(streamFN, "");
fileNumber++;
}


zipFN = Server.MapPath("~/Output") + "\\ZipFile.zip";
newZip.Save(zipFN);

}

Most of this code has to do with generating the text files for testing. The items in Bold declare the new ZIP file object, add the new files to it as they’re created and then saves the ZIP file. It worked within 15 minutes after I downloaded it unlike the other product that kept creating invalid ZIPs and had me running around the Net researching regular expression syntax which ended up not working anyway. I can’t fully vouch for DotNetZip as I’ve only just started using it but so far, I’m happy.


As I said, I also ended up doing a bit of research on regular expressions which are used to define search patterns in text samples such as file names and text files, etc.. An example would be the following:

^FILE.+\.txt$

This example could be used to search a list of files for any file names starting with “FILE” and ending with “.txt”. Using the REGEX class in .NET, you can quickly find matches for patterns like this one in blocks of text.

Regular expressions have a bit of a learning curve that goes with them so it helps to find a good introduction. One such introduction is Zytrax.com’s “Regular Expressions: A Simple User Guide and Tutorial” which I found to be clear and helpful in putting together the example above. Another cheat sheet can be found on RegExLib.com.


My other bit of fun for the weekend was coming home on Friday and finding that my copy of Visual Studio 2010 Professional had been delivered. (I know. I’m such a geek.) I’ve used it professionally at a couple different companies now and finally decided to get it for home use since I didn’t want to be limited to the 2010 Express versions. Amazon.com seems to have dropped the price on it and I’m guessing that has something to do with Visual Studio 11 being in beta. I’ll have to check that out at some point but for now, I’m happy with 2010 which is proven and stable.

Subscribe to the newsletter to be notified of new posts.

You can stay up-to-date on new articles and resources from Comeau Software Solutions by subscribing to our newsletter. You can opt-out at any time.