Martin
Martin

Reputation: 40603

Unzip file while reading it

I have hundreds of CSV files zipped. This is great because they take very little space but when it is time to use them, I have to make some space on my HD and unzip them before I can process. I was wondering if it is possible with .NET to unzip a file while reading it. In other words, I would like to open a zip file, start to decompress the file and as we go, process the file.

So there would be no need for extra space on my drive. Any ideas or suggestions?

Upvotes: 3

Views: 1005

Answers (4)

Jason Williams
Jason Williams

Reputation: 57952

Yes. Zip is a streamed format which means that you can use the data as you decompress it rather than having to decompress everything first.

With .net's System.IO.Compression classes you can apply similar compression as used in zip files (Deflate & GZip) to any stream you like, but if you want to work with actual zip format files you'll need a third party library like this one (sharpziplib).

Upvotes: 5

Marc Gravell
Marc Gravell

Reputation: 1064114

sharpziplib allows for stream-based decompression - see this related question - the item provides similar stream-based Read methods, so you can process each item like you would with any stream.

Upvotes: 1

Brendan Long
Brendan Long

Reputation: 54312

I'm not sure about zip files, but you could use GZ format with GZipSteam (works like any other input stream). Unfortunately, the entire System.IO.Compression namespace is only 2 classes (the other does DEFLATE).

EDIT: There's a class called ZipPackage. I'm not sure how if it will let you do decompression streaming, but it might be worth looking into.

Also, take a look at #ziplib.

Upvotes: 0

Assaf Lavie
Assaf Lavie

Reputation: 76103

A better solution might be to keep the files decompressed on the drive, but turn on compression on the file system level. This way you'll just be reading CSV files, and the OS will take care of making sure it doesn't take too much space.

Anyhoo, to answer your question, maybe the GZipStream class can help you.

Upvotes: 2

Related Questions