C# 文件压缩(WinForm、Asp.Net)

本文介绍了一种使用Gzip进行文件压缩和解压的方法,包括两个主要功能:一是将指定文件压缩为Gzip格式;二是将Gzip格式的文件还原为原始格式。提供了详细的C#代码实现。
 public static class GzipHelper
    {
        /// <summary>
        /// Gzip压缩
        /// </summary>
        /// <param name="sourceFile">待压缩文件</param>
        /// <param name="destinationFile">指定生成压缩后的文件</param>
        public static void GzipFile(string sourceFile, string destinationFile) {
            if (File.Exists(sourceFile) == false) throw new FileNotFoundException();
            byte[] buffer = null;
            FileStream sourceStream = null;
            FileStream destinationStream = null;
            GZipStream compressedStream = null;

            try {
                sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                buffer = new byte[sourceStream.Length];
                int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);
                if (checkCounter != buffer.Length) {
                    throw new ApplicationException();
                }
                destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write);
                compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true);
                compressedStream.Write(buffer, 0, buffer.Length);
            }
            catch (ApplicationException ex) {
                throw ex;
            }
            finally {
                if (sourceStream != null)
                    sourceStream.Close();

                if (compressedStream != null)
                    compressedStream.Close();

                if (destinationStream != null)
                    destinationStream.Close();
            }


        }

        /// <summary>
        /// Gzip压缩
        /// </summary>
        /// <param name="fInfo">待压缩文件,生成同名后缀".gz"的压缩文件</param>
        public static void GzipFile(FileInfo fInfo) {
            if (File.Exists(fInfo.ToString() + ".gz")) {
                File.Delete(fInfo.ToString() + ".gz");
            }
            if (fInfo.Exists == false) throw new FileNotFoundException();
            try {
                using (FileStream inFile = fInfo.OpenRead()) {
                    if ((File.GetAttributes(fInfo.FullName) & FileAttributes.Hidden)
                        != FileAttributes.Hidden & fInfo.Extension != ".gz") {
                        using (FileStream outFile = File.Create(fInfo.FullName + ".gz")) {
                            using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress)) {
                                byte[] tempInFile = new byte[inFile.Length];
                                inFile.Read(tempInFile, 0, (int)inFile.Length);
                                Compress.Write(tempInFile, 0, tempInFile.Length);
                                outFile.Flush();
                            }
                            outFile.Close();
                        }
                    }

                }
            }
            catch (Exception ex) {

                throw ex;
            }
            if (fInfo.Exists) {
                fInfo.Delete();
            }

        }

        /// <summary>
        /// Gzip解压
        /// </summary>
        /// <param name="sourceFile">待解压文件</param>
        /// <param name="destinationFile">指定生成解压后的文件</param>
        public static void UnGzipFile(string sourceFile, string destinationFile) {

            if (File.Exists(sourceFile) == false) throw new FileNotFoundException();
            FileStream sourceStream = null;
            FileStream destinationStream = null;
            GZipStream decompressedStream = null;
            byte[] quartetBuffer = null;
            try {
                sourceStream = new FileStream(sourceFile, FileMode.Open);
                decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true);
                quartetBuffer = new byte[4];
                int position = (int)sourceStream.Length - 4;
                sourceStream.Position = position;
                sourceStream.Read(quartetBuffer, 0, 4);
                sourceStream.Position = 0;
                int checkLength = BitConverter.ToInt32(quartetBuffer, 0);
                byte[] buffer = new byte[checkLength + 100];
                int offset = 0;
                int total = 0;
                while (true) {
                    int bytesRead = decompressedStream.Read(buffer, offset, 100);
                    if (bytesRead == 0)
                        break;
                    offset += bytesRead;
                    total += bytesRead;
                }
                destinationStream = new FileStream(destinationFile, FileMode.Create);
                destinationStream.Write(buffer, 0, total);
                destinationStream.Flush();
            }
            catch (ApplicationException ex) {
                throw ex;
            }
            finally {
                if (sourceStream != null)
                    sourceStream.Close();
                if (decompressedStream != null)
                    decompressedStream.Close();
                if (destinationStream != null)
                    destinationStream.Close();
            }


        }

        /// <summary>
        /// Gzip解压
        /// </summary>
        /// <param name="fInfo">待解压文件,生成同名去掉后缀".gz"的解压文件</param>
        public static void UnGZipFile(FileInfo fInfo) {
            if (fInfo.Exists == false) throw new FileNotFoundException();
            try {
                using (FileStream inFile = fInfo.OpenRead()) {
                    string curFile = fInfo.FullName;
                    string origName = curFile.Remove(curFile.Length - fInfo.Extension.Length);
                    using (FileStream outFile = File.Create(origName)) {
                        using (GZipStream Decompress = new GZipStream(inFile, CompressionMode.Decompress)) {
                            byte[] tempFile = new byte[100];
                            while (true) {
                                int size = Decompress.Read(tempFile, 0, 100);
                                if (size > 0)
                                    outFile.Write(tempFile, 0, size);
                                else
                                    break;
                            }
                            outFile.Flush();
                        }
                        outFile.Close();
                    }
                }
            }
            catch (Exception ex) {
                throw ex;
            }
        }
    }

转载于:https://www.cnblogs.com/whz881027/articles/2105979.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值