Search code examples
asp.netvb.netzipradupload

Rename Files during Extraction (Zip) process ASP.Net


i have a RadUpload that uploads zip file only, so i want to rename the each file during Extraction Process, i tried a way :

Protected Sub Upload(sender As Object, e As EventArgs)

    Dim extractPath As String = Server.MapPath("~/temp/")
    Dim file1 As String = RadUpload1.UploadedFiles(0).FileName

    ExtractFileToDirectory(file1, extractPath)

End Sub

Public Sub ExtractFileToDirectory(zipFileName As String, outputDirectory As String)

    Dim zip As ZipFile = ZipFile.Read(outputDirectory & zipFileName)
    Directory.CreateDirectory(outputDirectory)
    For Each e As ZipEntry In zip

        Dim NewName As String = Now().ToString("ddMMyyhhmmss")
        Dim newext As String = ".jpg"
        e.FileName = NewName + newext

        e.Extract(outputDirectory, ExtractExistingFileAction.OverwriteSilently)
    Next
End Sub

at the first it will rename and extract the first file but then gives this error:

[ It has been adjusted Group: Could not execute the census process. ]

any idea?


Solution

  • It seems there is a problem with outputDirectory

    Dim zip As ZipFile = ZipFile.Read(outputDirectory & zipFileName)
    Directory.CreateDirectory(outputDirectory)
    

    In the first line you are trying to read outputDirectory & zipFileName, in the second line you are trying to create that path.

    See MSDN, your code should be similar to

    Using zip As ZipArchive = ZipFile.OpenRead(zipFileName)
       For Each e As ZipArchiveEntry In zip.Entries
    
         Dim NewName As String = Now().ToString("ddMMyyhhmmss")
         Dim newext As String = ".jpg"
         NewName += newext
         e.ExtractToFile(Path.Combine(outputDirectory, NewName ))
    
       Next
     End Using
    

    NOTE: Using "ddMMyyhhmmss" as a file name you most likely will get an error if unzip takes less than 1 sec - either add ms, i.e. "ddMMyyhhmmssfff" or check if file name does not exist before extract.