Unzip .gz file using c# -
how unzip .gz file , save files in specific folder using c#?
this first time encounter .gz file. i've search in how unzip yet didn't work me. didn't unzip .gz file in specific folder. don't want used third party application.
can gave me sample code on how unzip it. save file in folder. thanks.
the following example msdn shows how use gzipstream class compress , decompress directory of files.
namespace zip { public class program { public static void main() { string directorypath = @"c:\users\public\reports"; directoryinfo directoryselected = new directoryinfo(directorypath); foreach (fileinfo filetocompress in directoryselected.getfiles()) { compress(filetocompress); } foreach (fileinfo filetodecompress in directoryselected.getfiles("*.gz")) { decompress(filetodecompress); } } public static void compress(fileinfo filetocompress) { using (filestream originalfilestream = filetocompress.openread()) { if ((file.getattributes(filetocompress.fullname) & fileattributes.hidden) != fileattributes.hidden & filetocompress.extension != ".gz") { using (filestream compressedfilestream = file.create(filetocompress.fullname + ".gz")) { using (gzipstream compressionstream = new gzipstream(compressedfilestream, compressionmode.compress)) { originalfilestream.copyto(compressionstream); console.writeline("compressed {0} {1} {2} bytes.", filetocompress.name, filetocompress.length.tostring(), compressedfilestream.length.tostring()); } } } } } public static void decompress(fileinfo filetodecompress) { using (filestream originalfilestream = filetodecompress.openread()) { string currentfilename = filetodecompress.fullname; string newfilename = currentfilename.remove(currentfilename.length - filetodecompress.extension.length); using (filestream decompressedfilestream = file.create(newfilename)) { using (gzipstream decompressionstream = new gzipstream(originalfilestream, compressionmode.decompress)) { decompressionstream.copyto(decompressedfilestream); console.writeline("decompressed: {0}", filetodecompress.name); } } } } } }
Comments
Post a Comment