标题: 使用C#2.0进行文件压缩和解压
- 银河 2005-09-17 10:55 阅读:4933
- 评论:9 查看评论 | 添加评论
在FCL2.0中增加了System.IO.Compression命名空间, 用以进行文件压缩和解压操作,如下所示:
[图片]using System;
[图片]using System.IO;
[图片]using System.IO.Compression;
[图片]
[图片]namespace Skyiv.Helper
[图片][图片][图片]{
[图片]    static class Zip
[图片][图片]    [图片]{
[图片]      public static void CompressFile(string sourceFile, string destinationFile)
[图片][图片]      [图片]{
[图片]        if (!File.Exists(sourceFile)) throw new FileNotFoundException();
[图片]      using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read))
[图片][图片]      [图片]{
[图片]        byte [] buffer = new byte[sourceStream.Length];
[图片]        int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);
[图片]        if (checkCounter != buffer.Length) throw new ApplicationException();
[图片]          using (FileStream destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write))
[图片][图片]          [图片]{
[图片]            using (GZipStream compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true))
[图片][图片]            [图片]{
[图片]              compressedStream.Write(buffer, 0, buffer.Length);
[图片]            }
[图片]          }
[图片]        }
[图片]      }
[图片]    
[图片]    public static void DecompressFile(string sourceFile, string destinationFile)
[图片][图片]      [图片]{
[图片]        if (!File.Exists( sourceFile)) throw new FileNotFoundException();
[图片]      using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open))
[图片][图片]        [图片]{
[图片]        byte [] 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];
[图片]          using (GZipStream decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true))
[图片][图片]          [图片]{
[图片]            int total = 0;
[图片]          for (int offset = 0; ; )
[图片][图片]          [图片]{
[图片]            int bytesRead = decompressedStream.Read(buffer, offset, 100);
[图片]            if (bytesRead == 0) break;
[图片]            offset += bytesRead;
[图片]            total += bytesRead;
[图片]          }
[图片]              using (FileStream destinationStream = new FileStream(destinationFile, FileMode.Create))
[图片][图片]              [图片]{
[图片]                destinationStream.Write(buffer, 0, total);
[图片]                destinationStream.Flush();
[图片]              }
[图片]        }
[图片]      }
[图片]    }
[图片]    }
[图片]}
[图片]
[图片]using System;
[图片]using System.IO;
[图片]using Skyiv.Helper;
[图片]
[图片]namespace Skyiv.Test
[图片][图片][图片]{
[图片]    sealed class TestMain
[图片][图片]    [图片]{
[图片]        static void Main()
[图片][图片]        [图片]{
[图片]            try
[图片][图片]            [图片]{
[图片][图片]                string [] nameList = [图片]{"zip.zip", "zip.cs", "zip.txt"};
[图片]                Zip.CompressFile(nameList[1], nameList[0]);
[图片]                Zip.DecompressFile(nameList[0], nameList[2]);
[图片]                foreach (string name in nameList)
[图片][图片]                [图片]{
[图片]                    Console.WriteLine("{0,-11}: {1,11:N0} bytes", name, new FileInfo(name).Length);
[图片]                }
[图片]            }
[图片]            catch (Exception ex)
[图片][图片]            [图片]{
[图片]                Console.WriteLine(ex.Message);
[图片]            }
[图片]        }
[图片]    }
[图片]}
[图片]
运行结果如下:
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文件的?

查看评论 | 添加评论
返回顶部 | 返回首页