Web ZIP archive uploader
ASP.NET ZIP archive uploader and extractor.
Demonstrates uploading and extracting ZIP archives into a folder at the web server. Uses simple static methods of the ZipArchive object to extract the contents of a ZIP archive uploaded to the ASP.NET web page.
The following code snippet shows the core of this sample:
C#
// construct physical paths string targetFolder = @"c:\temp"; string archiveFileName = Path.GetFileName(FileUploadControl.PostedFile.FileName); string archiveFilePath = Path.Combine(targetFolder, archiveFileName); // check the uploaded file if (FileUploadControl.PostedFile.ContentLength <= 0) throw new SampleException("No data uploaded."); // upload archive file first FileUploadControl.PostedFile.SaveAs(archiveFilePath); // unzip the archive ArchiveOperationResult res = ZipArchive.ExtractAll( archiveFilePath, targetFolder, TransferMethod.Copy, ActionOnExistingFiles.OverwriteAll); // display the number of extracted files UploadedFiles.Text = res.FilesAffected.ToString();
VB.NET
' construct physical paths Dim targetFolder As String = "c:\temp" Dim fileName As String = Path.GetFileName(FileUploadControl.PostedFile.FileName) Dim filePath As String = Path.Combine(targetFolder, fileName) ' check the uploaded file If FileUploadControl.PostedFile.ContentLength <= 0 Then Throw New SampleException("No data uploaded.") End If ' upload archive file first FileUploadControl.PostedFile.SaveAs(filePath) ' unzip the archive Dim res As ArchiveOperationResult = ZipArchive.ExtractAll( _ filePath, _ targetFolder, _ TransferMethod.Copy, _ ActionOnExistingFiles.OverwriteAll) ' display the number of extracted files UploadedFiles.Text = res.FilesAffected.ToString()
Note
If you need to upload larger ZIP archives, you have to increase the limit for HTTP request length using the following line in the web.config:
<system.web> ... <!-- Allow to upload ZIP files up to 100 MB with timeout up to 10 minutes --> <httpRuntime maxRequestLength="102400" executionTimeout="600" /> ... </system.web>