Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 817
Martin Avatar asked Jun 29 '26 10:06

Martin


2 Answers

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).

like image 60
Jason Williams Avatar answered Jul 02 '26 00:07

Jason Williams


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.

like image 29
Assaf Lavie Avatar answered Jul 01 '26 22:07

Assaf Lavie