///解压缩目录中的所有zip文件
using System;
using System.IO;
using System.IO.Compression;
using System.Collections;
using System.Collections.Generic;
private const long BUFFER_SIZE = 20480;
class FolderUnzip
{
static void main(string[] args)
{
string sourcepath=@"D:temp";
Queue<FileSystemInfo> Folders = new Queue<FileSystemInfo>(new DirectoryInfo(sourcepath).GetFileSystemInfos());
string copytopath =@"E:tt";
copytopath = (copytopath.LastIndexOf(Path.DirectorySeparatorChar) == copytopath.Length - 1) ? copytopath : (copytopath+Path.DirectorySeparatorChar) + Path.GetFileName(sourcepath);
Directory.CreateDirectory(copytopath);
while (Folders.Count > 0)
{
FileSystemInfo atom = Folders.Dequeue();
FileInfo zipfile = atom as FileInfo;
if (zipfile == null)
{
DirectoryInfo directory = atom as DirectoryInfo;
Directory.CreateDirectory(directory.FullName.Replace(sourcepath,copytopath));
foreach (FileSystemInfo nextatom in directory.GetFileSystemInfos())
Folders.Enqueue(nextatom);
}
else
{
if (zipfile.Name.IndexOf(".zip") > -1)
{
string zipfilename = zipfile.FullName;
string originalfilename = zipfilename.Replace(sourcepath,copytopath).Substring(0, (int)(zipfile.Length - 4));
if (!File.Exists(originalfilename))
{
FileStream sourceStream = null;
FileStream destinationStream = null;
gzipStream decompressedStream = null;
byte[] quartetBuffer = null;
try
{
// Read in the compressed source stream
sourceStream = new FileStream(zipfilename, FileMode.Open);
if(sourceStream.Length>4)
{
decompressedStream = new DeflateStream(sourceStream, CompressionMode.Decompress, true);
// Read the footer to determine the length of the destiantion file
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;
// Read the compressed data into the buffer
while (true)
{
int bytesRead = decompressedStream.Read(buffer, offset, 100);
if (bytesRead == 0)
break;
offset += bytesRead;
total += bytesRead;
}
destinationStream = new FileStream(originalfilename, FileMode.Create);
destinationStream.Write(buffer, 0, total);
destinationStream.Flush();
}
}
catch (ApplicationException)
{
continue;
}
finally
{
// Make sure we allways close all streams
if (sourceStream != null) sourceStream.Close();
if (decompressedStream != null) decompressedStream.Close();
if (destinationStream != null) destinationStream.Close();
}
}
}
}
}
}
}