Overriding Extract Directory

Apr 24, 2009 at 7:23 PM
So, I have been using DotNetZip and love it!

One issue I'm having, though, is some of the .zip files I'm interfacing with have files stored within directories in the .zip.  I know where I want the individual files to go, regardless of what the directory structure is in the .zip file.  How can I specify where the actual file goes, instead of the location to unzip to?

For example, the zip files has contents like:

[dir] 0000
[file]    --  0003_0294.jpg
[file]    --  0003_0394.jpg
...

I know I want C:\Temp\0003_0294.jpg in a specific location.  But if I call

ZipFile.Extract( "0000/0003_0294.jpg", "C:\\Temp" );

The file is location is C:\Temp\0000\0003_0294.jpg.

Thanks for the help!

--Chris
Apr 24, 2009 at 8:00 PM
Edited Apr 24, 2009 at 8:03 PM

Using

zip1 As ZipFile = ZipFile.Read(ZipToUnpack)

 

 

 

Dim e As ZipEntry

 Dim newfilename As String

 For Each e In zip1

 Dim s As String = e.FileName

 Dim pathend As Integer = s.LastIndexOf("/") + 1

 Dim filename As String = s.Substring(pathend)
newfilename =  filename

 

 

 e.FileName = newfilename
Dim ex As ExtractExistingFileAction = ExtractExistingFileAction.OverwriteSilently

 

 

e.Extract(UnpackDirectory, ex)
next

this is working for me, though Im using ver 1.8 so your e.extract is a little different in earlier versions I think, anyway filename gives you the actual filename minus all the path(s). UnpackDirectory would be c:/temp in your case probably

 

 

 

 

 

 

Apr 24, 2009 at 8:14 PM
Unfortunately I'm using v1.6, so there are a few problems with the code:

    - e.Filename is read-only
    - I tried calling zip1.UpdateFile( e.Filename, "" ) to change the directory, but this modifies the collection, which invalidates the enumerator on the next pass

Also, is there a way to get a ZipEntry instance without enumerating over the collection?

--Chris
Apr 24, 2009 at 8:27 PM
I just happened on this site yesterday so thats about the limit of my knowledge , sorry , pretty sure there is a way to get the zipentry by name(or index I think) , but you'd have to know the filename ahead of time or loop thru the zip file to get all the filenames/indexes anyway I think, 1.8 seems to be fine so far for me so you might want to upgrade unless youve got a body of code using the older version I guess
Coordinator
Apr 28, 2009 at 6:07 PM
Flyncode, to answer your question, if the zip file encodes a location for a particular entry, then you have to take special action when extracting if you do NOT  want to extract into that location. As you saw, you can specify the root directory for the extraction, but the path for the ZipEntry is used when extracting.

I can see a couple easy options:
  • extract the entry to a FileStream, which you open to the path you desire.
  • Extract to a temp location and then rename (remap) files.

Using later versions of the library you can change the FileName on the entry and then extract, but this won't work on v1.6, as you already pointed out.