Adding a File to a Zipped File using

When manipulating zip file through a web application in the past it has been tedious and time consuming, however with the new System.IO.Compression in Framework 4.5 it drastically minimized the effort and time to use it. There are many examples on the internet showing you how to do this but today I want to provide the code to do something that is very specific.

Let me provide the scenario. I have zipped file that contains a program along with some supporting files, now when I send it to the user I want to add a text file that contains the receipt information along with a specific license code that the user needs. I could send them the zip file and then send them a separate file or email but I want the user to always have it as part of the download. This would have been cumbersome in the old way but with the new way it is very easy.

Here is the code.

First add this to the top.

[code language=”vb”]
Imports System.IO.Compression
Imports System.IO

then add this to the Sub

[code language=”vb”]

‘Name of the zipped file you want to add a file to
Using zipToOpen As FileStream = New FileStream("d:\", FileMode.Open)
Using archive As ZipArchive = New ZipArchive(zipToOpen, ZipArchiveMode.Update)
‘This is the new name of the file you want to add
Dim readmeEntry As ZipArchiveEntry = archive.CreateEntry("Serial.txt")
Using writer As StreamWriter = New StreamWriter(readmeEntry.Open())
‘Begin to add the information to the readme file
writer.WriteLine("Serial Number")
End Using
End Using
End Using

See the comments inline for better understanding. You can see that there are other things you could use this for. In another article I will show you how to stream this back to the user and save it to an email and send it.

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , | Posted under Programming/Coding | RSS 2.0

Author Spotlight

David Bauernschmidt

I live in the historical triangle of Virginia where I am married with two daughters. I have spent over 13 years working for a Fortune 500 company in the computer area. I started in VB 6.0 and by the time I ended my employment I was supervising a development team where we built many web applications. When my first daughter was born I wanted to spend more time with her so I left and became a programmer analyst for local government as well as launch my own company. Since then I have grown James River Webs into a profitable web design and application company helping small businesses create a big presence on the internet. As an employee I have created web application used by citizens and other companies. I enjoy fly fishing, and spending time with my family. I also enjoy learning new approaches and development tools when it comes to developing applications.

Leave a Reply

Your email address will not be published. Required fields are marked *