Created
June 13, 2021 17:14
-
-
Save im-ikao/913d22ec49ed2d8365eb1022133bc052 to your computer and use it in GitHub Desktop.
[.GZ File Compression/Decompression Extension]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class CompressionExtensions | |
{ | |
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); | |
} | |
} | |
} | |
} | |
} | |
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); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment