`

GZipStream / DeflateStream 壓縮 / 解壓縮

    博客分类:
  • .NET
 
阅读更多
http://www.dotblogs.com.tw/yc421206/archive/2009/01/18/6869.aspx

.NET提供了兩種壓縮資料流一個是GZipStream,另一個是DeflateStream,使用壓縮資料流要注意幾項:
1.這兩個演算法最大只能對4G的資料進行壓縮。
2.如果想要將檔案分給其它人使用,且要讓它們能夠用ZIP解壓縮時,請用GZipStream。


壓縮檔案步驟:
1.引用System.IO及System.IO.Compression類別
2.開啟欲壓縮的檔案
//開啟來源檔
FileStream SourceFile = File.OpenRead(inFile);
3.建立壓縮後的檔案
//建立目的地檔
FileStream DestnFile = File.Create(outFile);

4.引用壓縮資料流GZipStream,引用壓縮參數
//引用壓縮類別
GZipStream myGZip = new GZipStream(DestnFile, CompressionMode.Compress);
5.寫入資料至壓縮資料流
//設定讀被壓縮(來源檔)變數
int ByteFile = SourceFile.ReadByte();
while (ByteFile != -1)
{
       //寫入myGZip資料流(壓縮)
       myGZip.WriteByte((byte)ByteFile);
       ByteFile = SourceFile.ReadByte();
}

6.釋放資源
//釋放資源
myGZip.Dispose();
DestnFile.Dispose();
SourceFile.Dispose();



解壓縮檔案步驟:
1.引用System.IO及System.IO.Compression類別
2.開啟欲解壓縮的檔案
//開啟來源檔
FileStream SourceFile = File.OpenRead(inFile);
3.建立壓縮後的檔案
//建立目的地檔
FileStream DestnFile = File.Create(outFile);
4.引用壓縮資料流GZipStream,引用解壓縮參數
//引用解壓縮類別
GZipStream myGZip = new GZipStream(SourceFile, CompressionMode.Decompress);
5.寫入資料至目的地檔案
//設定讀解壓縮(來源檔)變數
int ByteFile = myGZip.ReadByte();
while (ByteFile != -1)
{
       //寫資料至目的地檔(解壓縮)
       DestnFile.WriteByte((byte)ByteFile);
       ByteFile = myGZip.ReadByte();
}
6.釋放資源
//釋放資源
myGZip.Dispose();
DestnFile.Dispose();
SourceFile.Dispose();




Imports System.IO
Imports System.IO.Compression
'引用命名空间
    ''' <summary>
    ''' 压缩档案
    ''' </summary>
    ''' <param name="sourceFile">源文件</param>
    ''' <param name="destinationFile">压缩后文件</param>
    ''' <remarks></remarks>
    Public Function CompressFile(ByVal sourceFile As String, ByVal destinationFile As String) As Boolean
        If Not File.Exists(sourceFile) Then Throw New FileNotFoundException
        Dim sourceStream As FileStream = Nothing
        Dim destinationStream As FileStream = Nothing
        Dim compressedStream As GZipStream = Nothing
        Try
            sourceStream = New FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read)
            Dim buffer(sourceStream.Length - 1) As Byte
            Dim checkCounter As Integer = sourceStream.Read(buffer, 0, buffer.Length)
            If checkCounter <> buffer.Length Then 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 ex As ApplicationException
            Return False
        Finally
            If sourceStream IsNot Nothing Then sourceStream.Close()
            If compressedStream IsNot Nothing Then compressedStream.Close()
            If destinationStream IsNot Nothing Then destinationStream.Close()
        End Try
        Return True
    End Function
    ''' <summary>
    ''' 解压缩档案
    ''' </summary>
    ''' <param name="sourceFile">压缩过的文件</param>
    ''' <param name="destinationFile">输出源文件</param>
    ''' <remarks></remarks>
    Public Function DecompressFile(ByVal sourceFile As String, ByVal destinationFile As String) As Boolean
        If Not File.Exists(sourceFile) Then Throw New FileNotFoundException
        Dim sourceStream As FileStream = Nothing
        Dim destinationStream As FileStream = Nothing
        Dim decompressedStream As GZipStream = Nothing
        Dim quartetBuffer(4) As Byte
        Try
            sourceStream = New FileStream(sourceFile, FileMode.Open)
            decompressedStream = New GZipStream(sourceStream, CompressionMode.Decompress, True)
            Dim position As Integer = sourceStream.Length - 4
            sourceStream.Position = position
            sourceStream.Read(quartetBuffer, 0, 4)
            sourceStream.Position = 0
            Dim checkLength As Integer = BitConverter.ToInt32(quartetBuffer, 0)
            Dim buffer(checkLength + 100) As Byte
            Dim offset, total As Integer
            While (True)
                Dim bytesRead As Integer = decompressedStream.Read(buffer, offset, 100)
                If bytesRead = 0 Then Exit While
                offset += bytesRead
                total += bytesRead
            End While
            destinationStream = New FileStream(destinationFile, FileMode.Create)
            destinationStream.Write(buffer, 0, total)
            destinationStream.Flush()
        Catch ex As ApplicationException
            Return False
        Finally
            If sourceStream IsNot Nothing Then sourceStream.Close()
            If decompressedStream IsNot Nothing Then decompressedStream.Close()
            If destinationStream IsNot Nothing Then destinationStream.Close()
        End Try
        Return True
    End Function
分享到:
评论

相关推荐

    C++ 压缩解压缩库

    C++ 压缩解压缩库,VS2012版本。我只封装了解压缩库,如果需要可以自己封装下压缩的。代码齐全。

    .net 利用 GZipStream 压缩和解压缩

    NULL 博文链接:https://dampce032.iteye.com/blog/1553646

    如何压缩多个文件\文件夹(GZipStream and C#)

    在.Net Framework 2.0 中添加了System.IO.Compression 类来实现对文件/文件夹的压缩/解压(GZipStream方法),包括文档,代码,类文件

    序列化和Zip压缩

    本dll提供了两种压缩方式,GZipStream和DeflateStream,在使用Webservice作为服务端的系统中,提高传输性能是比较关键的,而提高传输性能,对传输数据进行压缩是很重要的方法。 使用.Net自带压缩算法,压缩率很大...

    C#使用GZipStream解压缩数据文件的方法

    主要介绍了C#使用GZipStream解压缩数据文件的方法,实例分析了C#中GZipStream方法的原理与使用技巧,需要的朋友可以参考下

    vb.net 利用.net自带的GZipStream压缩或者解压文件的代码,不需要任何第三方控件

    网上很少有用VB写的压缩文件的代码,但是,在网络传输,文件下载,打包发布等等方面的需求又比较多,所以,借鉴了一下C#代码的例子,改造成了VB用的类。另外加上了多层文件夹压缩解压。但是,因为时间有限,只是将...

    c# 压缩及解压缩源码

    压缩及解压缩

    GZipStream压缩文件称gzip格式

    支持同时压缩多文件,可以选择压缩后的文件放置的位置。

    C#使用GZipStream实现文件的压缩与解压

    主要为大家详细介绍了C#使用GZipStream实现文件的压缩与解压,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    C#实现页面GZip或Deflate压缩的方法

    System.IO.Compression下有两个可用于页面压缩的类,GZipStream和 DeflateStream. 在页面被传输之前,需要获取发出请求的客户端所采用的解码形式。 可以通过Request.Headers[“Accept-Encoding”]来获取。 在页面被...

    gzipstream:gzipstream允许Python处理来自流媒体源的多部分gzip文件-python source file

    作为用法的示例, examples/streaming_commoncrawl_from_s3.py显示了如何使用gzipstream增量处理gzip压缩的Web存档(WARC)文件。 该文件的大小几乎为1 GB,是从2014-15 Common Crawl数据集中随机选择的,并托管在...

    C#实现GZip压缩和解压缩入门实例

    主要是因为GZipStream的构造函数中第一个需要传入一个Stream,第二个是指定操作方式:压缩还是解压缩。 当时的疑问点主要有: 1.我传入的Stream是包含未压缩数据的Stream吗?2.我解压时是从一个压缩流中读取数据后再...

    用.NET 2.0压缩/解压功能处理大型数据

    这个新名称空间提供了两个数据压缩类:DeflateStream和GZipStream。这两个压缩类都支持无损压缩和解压,其设计目的是为了处理流式数据的压缩和解压问题。压缩是减少数据大小的有效办法。例如,如果你有巨大量的数据...

    【.Net 】Zip操作库

    - WCF服务,它接收一个zip文件作为附件,并动态的zip解压缩到一个流分析 - 一个老派的ASP(VBScript)中的应用,产生了DotNetZIp通过COM接口一个ZIP文件 - 一个Windows窗体应用程序,读取或更新ODS的文件 - 从流...

    压缩解压数据 dll

    c++ 调用 gzipstream 对数据进行加压或者解压

    Unity.IO.Compression:Unity的System.IO.Compression端口

    Unity.IO.Compression 这是微软的代码端口从。... 这似乎是使GZipStream和DeflateStream类在Unity中工作的最干净,最稳定的方法。 在找到Unity Asset Store上的插件。 由,请与我们联系以获取问题。

    asp.net在线压缩文件

    网络传输文件过程中,如果文件过大将会影响文件传送的效果和速度,如果将文件压缩之后再上传,不但可以提高传送速度,还可以节省大量的时间。本实例通过asp.net web...本实例主要使用了FileStream类和GZipStream类。

Global site tag (gtag.js) - Google Analytics