Unzip / Unpack / Decompress to specific folder

Feb 7, 2011 at 9:53 AM

To the devs in the clouds

Using ZipEntry.Extract

Problem is that when I extract the zip file, it unpacks it by default into a folder with date as name, how can I specify what the final folder name should be into which the files must be extracted. When I use the zipentry.extract it extracts to the correct folder, but adds a folder with date as name in the unpack folder. I need it to unpack directly into the folder I specified.

Your help will be very much appreciated as I am only seeing a workaround of checking the "unpack folder\date" and moving all the files to "unpack folder" and then deleting date folder.

Thank you

regards The_O


            Dim ZipBackupFileName As String = String.Empty
            Dim ZipFilePath As String = String.Empty
            Using zippy As ZipFile = ZipFile.Read(ZipToUnpack)
                Dim e As ZipEntry
                Dim ZipFileName As String = Path.GetFileNameWithoutExtension(zippy.Name)
                ZipFilePath = zippy.Name

                For Each e In zippy
                    If extFilter IsNot Nothing And extFilter.Length > 0 Then
                        For Each ext As String In extFilter
                            If e.FileName.EndsWith(ext) Then
                                e.Extract(UnpackDirectory & "\" & ZipFileName, ExtractExistingFileAction.OverwriteSilently)
                                Exit For
                            End If
                        e.Extract(UnpackDirectory & "\" & ZipFileName, ExtractExistingFileAction.OverwriteSilently)
                    End If
                UnpackDirectory = UnpackDirectory & "\" & ZipFileName
                MoveDirectory(Directory.GetDirectories(UnpackDirectory)(0), UnpackDirectory)  'NOTE1
                ZipBackupFileName = Path.GetFileName(zippy.Name)
            End Using

'NOTE1 : The MoveDirectory method moves all files from one folder (recursively) to another folder


Feb 14, 2011 at 2:01 PM

ZipEntry.Extract extracts into the directory that is specified in the ZipEntry itself.   ZipEntry.Extract() does not append a date to the directory where you specify, to extract the entry.

It seems to me that the entries in your zipfile have FileName properties that include dates.  If you don't want that, then you need to either construct the zipfile differently, or you need to change the FileName property on the ZipEntry, before calling Extract().  If you have a ZipEntry with FileName "2011-02-12/folder1/Image1.jpg" and call Extract(), you will get a path in your filesystem of  "2011-02-12/folder1/Image1.jpg" .  If you change the FileName on the ZipEntry to "SomethingElse.txt", and then call ZipEntry.Extract() you will get a path in your filesystem of "SomethingElse.txt".