Casey ScriptFu Pharr
Casey ScriptFu Pharr

Reputation: 1670

How to use System.IO.Compression to rename file during the unpacking process?

I have a pretty basic or general question, but am having some issues finding an answer for it. I am using the System.IO.Compression to manage files in a drop folder that will be .zip file types. How can I unpack the files, and rename all of the packaged files during the opening process? I see how to unpack them, but will possibly clobber or overwrite other files that are duplicates if I can't rename them.

Is the only way to keep track of the files you touch by using the "Entries" method, and then send to a temp, and then rename, and place them where they are suppose to be? I would assume I could do the naming during the opening process, and not have the extra steps. If not, or I am missing something, I apologize for the post.

Upvotes: 0

Views: 3209

Answers (2)

PetarPenev
PetarPenev

Reputation: 339

Please see the ZipFileExtraction entry at MSDN. You could pass false as the overwrite parameter, then catch the IO Exception and rename the file accordingly. Of course, the IO exception can be thrown for other reasons as specified in the article so you have to differentiate between them according to your use case.

Upvotes: 1

Ben Voigt
Ben Voigt

Reputation: 283614

The ZipArchiveEntry class has an ExtractToFile(string destinationFileName) method (well, it's actually an extension method) that lets you save to a file of any name, not just the filename stored in the zip file.

It definitely will be more work, because you'll have to create subdirectories yourself before extracting file content to them, but gives you a lot more control than extracting the whole archive in one go.

Upvotes: 2

Related Questions