-1

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?

NoobMaster
  • 27
  • 13

2 Answers2

1

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.

user2316116
  • 6,726
  • 1
  • 21
  • 35
  • exactly what i want but when i copy your code it says ZipArchive in not Recognized – NoobMaster Jan 09 '17 at 16:57
  • i also added this reference [ Imports System.IO.Compression.ZipArchive ] but does not accept the reference – NoobMaster Jan 09 '17 at 17:29
  • after i changed framework version to 4.5 then your code worked like a charm , thanks for your help, you saved my day – NoobMaster Jan 10 '17 at 06:28
  • If you want to keep old .net version, you can still use your old code but you should change the behaviour of using outputDirectory – user2316116 Jan 10 '17 at 08:36
  • This works great! I was having issues extracting a file with too long of a name so this allowed me to rename it to something else. – Tilting Code Jun 22 '18 at 17:05
0

Since you are expecting to extract to a "temp" folder, you really should be using the System temporary folder. Writing data inside your web app is a bad practice.

Use the following to retrieve the system-defined temporary folder

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

Dim extractPath As String = System.IO.Path.GetTempPath() ' Better way to store temporary files
Dim file1 As String = RadUpload1.UploadedFiles(0).FileName

ExtractFileToDirectory(file1, extractPath)

End Sub

Then inside ExtractFileToDirectory function you create a brand new temporary folder (use Guid.NewGuid().ToString() to generate a unique folder name) and unzip files as explained by @2316116, using the ExtractToFile method.

Following this approach saves you from nasty errors occurring when multiple zip files are unpacking at the very same time.