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 Microsoft.net 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.
Imports System.IO.Compression Imports System.IO
then add this to the Sub
'Name of the zipped file you want to add a file to Using zipToOpen As FileStream = New FileStream("d:\even.zip", 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") writer.WriteLine("================================") 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!