Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How to read a text file from GZip

Tags:

c#

Here is my problem, i'm trying to make minecraft classic server and i'm using text system to make allow list for each map, problem is text system makes a file for each map and we got around 15k maps in total, so if 1k of players add allow list to their maps, it would be hard to upload / move server to another host. i want to make a zip file in main folder of my software and add each text file to it and also making it readable with system, i want to know how to read a file from GZip, and how to compress files also.

Thanks

like image 724
111WARLOCK111 Avatar asked Apr 05 '13 09:04

111WARLOCK111


2 Answers

Here is my very easy working code. No temporary file

using (FileStream reader = File.OpenRead(filePath))
    using (GZipStream zip = new GZipStream(reader, CompressionMode.Decompress, true))
        using (StreamReader unzip = new StreamReader(zip))
            while(!unzip.EndOfStream)
                ReadLine(unzip.ReadLine());
like image 105
Xavius Pupuss Avatar answered Sep 18 '22 10:09

Xavius Pupuss


If you want to avoid creating temporary files, you can use this:

using (Stream fileStream = File.OpenRead(filePath),
              zippedStream = new GZipStream(fileStream, CompressionMode.Decompress))
{
    using (StreamReader reader = new StreamReader(zippedStream))
    {
        // work with reader
    }
}
like image 27
Juozas Kontvainis Avatar answered Sep 22 '22 10:09

Juozas Kontvainis