使用C#2.0进行文件压缩和解压

在FCL2.0中增加了System.IO.Compression命名空间, 用以进行文件压缩和解压操作,如下所示:
None.gif using  System;
None.gif
using  System.IO;
None.gif
using  System.IO.Compression;
None.gif
None.gif
namespace  Skyiv.Helper
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static class Zip
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
public static void CompressFile(string sourceFile, string destinationFile)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
if (!File.Exists(sourceFile)) throw new FileNotFoundException();
InBlock.gif      
using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read))
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
byte [] buffer = new byte[sourceStream.Length];
InBlock.gif        
int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);
InBlock.gif        
if (checkCounter != buffer.Length) throw new ApplicationException();
InBlock.gif          
using (FileStream destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write))
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif            
using (GZipStream compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif              compressedStream.Write(buffer, 
0, buffer.Length);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif          }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif      }

InBlock.gif    
InBlock.gif    
public static void DecompressFile(string sourceFile, string destinationFile)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
if (!File.Exists( sourceFile)) throw new FileNotFoundException();
InBlock.gif      
using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
byte [] quartetBuffer = new byte[4];
InBlock.gif        
int position = (int)sourceStream.Length - 4;
InBlock.gif        sourceStream.Position 
= position;
InBlock.gif        sourceStream.Read(quartetBuffer, 
04);
InBlock.gif        sourceStream.Position 
= 0;
InBlock.gif        
int checkLength = BitConverter.ToInt32(quartetBuffer, 0);
InBlock.gif        
byte[] buffer = new byte[checkLength + 100];
InBlock.gif          
using (GZipStream decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true))
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif            
int total = 0;
InBlock.gif          
for (int offset = 0; ; )
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif            
int bytesRead = decompressedStream.Read(buffer, offset, 100);
InBlock.gif            
if (bytesRead == 0break;
InBlock.gif            offset 
+= bytesRead;
InBlock.gif            total 
+= bytesRead;
ExpandedSubBlockEnd.gif          }

InBlock.gif              
using (FileStream destinationStream = new FileStream(destinationFile, FileMode.Create))
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif                destinationStream.Write(buffer, 
0, total);
InBlock.gif                destinationStream.Flush();
ExpandedSubBlockEnd.gif              }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif using  System;
None.gif
using  System.IO;
None.gif
using  Skyiv.Helper;
None.gif
None.gif
namespace  Skyiv.Test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
sealed class TestMain
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
string [] nameList = dot.gif{"zip.zip""zip.cs""zip.txt"};
InBlock.gif                Zip.CompressFile(nameList[
1], nameList[0]);
InBlock.gif                Zip.DecompressFile(nameList[
0], nameList[2]);
InBlock.gif                
foreach (string name in nameList)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Console.WriteLine(
"{0,-11}: {1,11:N0} bytes", name, new FileInfo(name).Length);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(ex.Message);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
运行结果如下:
zip.zip    :         773 bytes
zip.cs     :       2,196 bytes
zip.txt    :       2,196 bytes
但是,如果有使用Zip.DecompressFile()方法去解压标准的ZIP文件,就会出现以下错误:
GZip 头中的幻数不正确。请确保正在传入 GZip 流。
此外,使用Zip.CompressFile()方法也不能将多个文件放入一个ZIP包。
看来,FCL2.0还是不能处理标准的ZIP文件,我目前是使用第三方的ICSharpZipLib来处理ZIP文件:
http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
不知各位是使用什么方案来处理ZIP文件的?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值