Click here to Skip to main content
15,799,398 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have a bunch of folders that users have uploaded via a zipped file. I extract the folder contents create a new folder and save the contents to the folder on the hard drive.

I currently save the original zipped folder and the extracted content but that is not a good method because if they wanted to delete or move content around in the extracted files it would not reflect in the saved zipped that was originally uploaded.

When a user request the contents of the folder I feed them the original zipped file but as you can see there is a problem if they made changes to the extracted content.

I am trying to see if there is a way to zip the extracted folder and present the newly zipped folder to the client for download without saving the newly zipped folder.

Any help would be a great help. Thanks in advance.

You can ZIP or UNZIP the folder with that library:

It's OpenSource and entirely wrote in C#
Share this answer
Member 7790032 2-Aug-11 11:10am    
Thanks for the input I am using that library to extract the original file and using it to zip them again but I had another concern about loose files that haven't been deleted due to some unforeseen issue.
Astolf 2-Aug-11 11:11am    
You don't need to save the file to disk.

If you have the stream, you can send directly to the client changing the web header in the PostBack.
Use SharpZipLib[^] to zip your folder on the fly and present it to the user.
Share this answer
Member 7790032 2-Aug-11 11:08am    
Thanks for the response. I have a function that returns the zipped file using that library ,below is my function call
<pre> Public Function ZipFiles(ByVal inputFolderPath As String, ByVal outputPathAndFile As String) As String
Dim outPath As String = Nothing

Dim ar As ArrayList = GenerateFileList_Zipping(inputFolderPath)
' generate file list
Dim TrimLength As Integer = (Directory.GetParent(inputFolderPath)).ToString().Length
' find number of chars to remove // from orginal file path
TrimLength += 1
'remove '\'
Dim ostream As FileStream
Dim obuffer As Byte()
outPath = inputFolderPath & "\" & outputPathAndFile
Dim oZipStream As New ZipOutputStream(File.Create(outPath))
' create zip stream

' maximum compression
Dim oZipEntry As ZipEntry
For Each Fil As String In ar
' for each file, generate a zipentry
oZipEntry = New ZipEntry(Fil.Remove(0, TrimLength))

If Not Fil.EndsWith("/") Then
' if a file ends with '/' its a directory
ostream = File.OpenRead(Fil)
obuffer = New Byte(ostream.Length - 1) {}
ostream.Read(obuffer, 0, obuffer.Length)
oZipStream.Write(obuffer, 0, obuffer.Length)
End If

Catch ex As Exception

If (File.Exists(outputPathAndFile)) Then
End If
End Try

Return outPath
End Function </pre>

but I have to store the file on the server to be downloaded. I don't want a situation of having a bunch of temporary files on the server that wasn't deleted due to some issue
Member 7790032 2-Aug-11 11:13am    
Is there a way to save it to the file stream and directly flush the created zip to the browser without saving a hard copy and if this method would be efficient and not bring down my server...
[no name] 2-Aug-11 14:59pm    
You can try writing the file to Response.OutputStream, I haven't tried though, but it should work.
Philippe Mori 2-Aug-11 19:53pm    
I thing that if you have an exception the stream might not be closed with the above code which will probably prevent the file from being deleted.
In some case, it might make sense to delete files after a while (say 5 minutes) so that if the user try to download it twice for any reason you won't have to recreate the original zip file.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900