如何压缩多个文件.docx
- 文档编号:23231681
- 上传时间:2023-05-15
- 格式:DOCX
- 页数:24
- 大小:18.97KB
如何压缩多个文件.docx
《如何压缩多个文件.docx》由会员分享,可在线阅读,更多相关《如何压缩多个文件.docx(24页珍藏版)》请在冰豆网上搜索。
如何压缩多个文件
如何压缩多个文件/文件夹
用例:
1.压缩
1 stringfolder=Path.Combine(Server.MapPath("~"),"TestCompress");
2 stringfile="file1.txt";
3 stringzip="myzip";
4
5 SampleCompress.Compress(Path.Combine(folder,file),Path.Combine(folder,zip));
2.解压
1 stringfolder=Path.Combine(Server.MapPath("~"),"TestCompress");
2 stringfile="file1.txt";
3 stringzip="myzip";
4
5 SampleCompress.Decompress(Path.Combine(folder,zip),Path.Combine(Path.Combine(folder,"zipfolder"),file));
由代码和使用例子我们可以了解到,Code1只是支持单个文本文件的压缩/解压,代码非常简单,但是却实际上却没什么用途,功能太少,只是让你有个初步的认识.下面介绍Code2来实现本文的主题内容.
Code2:
publicclassGZip
{
///
///Compress
///
///
///
///
publicstaticGZipResultCompress(stringlpSourceFolder,stringlpDestFolder,stringzipFileName)
{
returnCompress(lpSourceFolder,"*.*",SearchOption.AllDirectories,lpDestFolder,zipFileName,true);
}
///
///Compress
///
///
///
///
///
///
///
publicstaticGZipResultCompress(stringlpSourceFolder,stringsearchPattern,SearchOptionsearchOption,stringlpDestFolder,stringzipFileName,booldeleteTempFile)
{
DirectoryInfodi=newDirectoryInfo(lpSourceFolder);
FileInfo[]files=di.GetFiles("*.*",searchOption);
returnCompress(files,lpSourceFolder,lpDestFolder,zipFileName,deleteTempFile);
}
///
///Compress
///
///
///
///
///storedinthezipfile.Forexample,iflpBaseFolderis'C:
zipTestFiles',andthereisafile
///'C:
zipTestFilesfolder1sample.txt'inthe'files'array,therelativepathforsample.txt
///willbe'folder1/sample.txt'
///
///
publicstaticGZipResultCompress(FileInfo[]files,string[]folders,stringlpBaseFolder,stringlpDestFolder,stringzipFileName)
{
//supportcompressfolder
IList
foreach(FileInfoliinfiles)
list.Add(li);
foreach(stringstrinfolders)
{
DirectoryInfodi=newDirectoryInfo(str);
foreach(FileInfoinfoindi.GetFiles("*.*",SearchOption.AllDirectories))
{
list.Add(info);
}
}
returnCompress(list.ToArray(),lpBaseFolder,lpDestFolder,zipFileName,true);
}
///
///Compress
///
///
///
///storedinthezipfile.Forexample,iflpBaseFolderis'C:
zipTestFiles',andthereisafile
///'C:
zipTestFilesfolder1sample.txt'inthe'files'array,therelativepathforsample.txt
///willbe'folder1/sample.txt'
///
///
publicstaticGZipResultCompress(FileInfo[]files,stringlpBaseFolder,stringlpDestFolder,stringzipFileName)
{
returnCompress(files,lpBaseFolder,lpDestFolder,zipFileName,true);
}
///
///Compress
///
///
///
///storedinthezipfile.Forexample,iflpBaseFolderis'C:
zipTestFiles',andthereisafile
///'C:
zipTestFilesfolder1sample.txt'inthe'files'array,therelativepathforsample.txt
///willbe'folder1/sample.txt'
///
///
///
publicstaticGZipResultCompress(FileInfo[]files,stringlpBaseFolder,stringlpDestFolder,stringzipFileName,booldeleteTempFile)
{
GZipResultresult=newGZipResult();
try
{
if(!
lpDestFolder.EndsWith(""))
{
lpDestFolder+="";
}
stringlpTempFile=lpDestFolder+zipFileName+".tmp";
stringlpZipFile=lpDestFolder+zipFileName;
result.TempFile=lpTempFile;
result.ZipFile=lpZipFile;
if(files!
=null&&files.Length>0)
{
CreateTempFile(files,lpBaseFolder,lpTempFile,result);
if(result.FileCount>0)
{
CreateZipFile(lpTempFile,lpZipFile,result);
}
//deletethetempfile
if(deleteTempFile)
{
File.Delete(lpTempFile);
result.TempFileDeleted=true;
}
}
}
catch//(Exceptionex4)
{
result.Errors=true;
}
returnresult;
}
privatestaticvoidCreateZipFile(stringlpSourceFile,stringlpZipFile,GZipResultresult)
{
byte[]buffer;
intcount=0;
FileStreamfsOut=null;
FileStreamfsIn=null;
GZipStreamgzip=null;
//compressthefileintothezipfile
try
{
fsOut=newFileStream(lpZipFile,FileMode.Create,FileAccess.Write,FileShare.None);
gzip=newGZipStream(fsOut,CompressionMode.Compress,true);
fsIn=newFileStream(lpSourceFile,FileMode.Open,FileAccess.Read,FileShare.Read);
buffer=newbyte[fsIn.Length];
count=fsIn.Read(buffer,0,buffer.Length);
fsIn.Close();
fsIn=null;
//compresstothezipfile
gzip.Write(buffer,0,buffer.Length);
result.ZipFileSize=fsOut.Length;
result.CompressionPercent=GetCompressionPercent(result.TempFileSize,result.ZipFileSize);
}
catch//(Exceptionex1)
{
result.Errors=true;
}
finally
{
if(gzip!
=null)
{
gzip.Close();
gzip=null;
}
if(fsOut!
=null)
{
fsOut.Close();
fsOut=null;
}
if(fsIn!
=null)
{
fsIn.Close();
fsIn=null;
}
}
}
privatestaticvoidCreateTempFile(FileInfo[]files,stringlpBaseFolder,stringlpTempFile,GZipResultresult)
{
byte[]buffer;
intcount=0;
byte[]header;
stringfileHeader=null;
stringfileModDate=null;
stringlpFolder=null;
intfileIndex=0;
stringlpSourceFile=null;
stringvpSourceFile=null;
GZipFileInfogzf=null;
FileStreamfsOut=null;
FileStreamfsIn=null;
if(files!
=null&&files.Length>0)
{
try
{
result.Files=newGZipFileInfo[files.Length];
//openthetempfileforwriting
fsOut=newFileStream(lpTempFile,FileMode.Create,FileAccess.Write,FileShare.None);
foreach(FileInfofiinfiles)
{
lpFolder=fi.DirectoryName+"";
try
{
gzf=newGZipFileInfo();
gzf.Index=fileIndex;
//readthesourcefile,getitsvirtualpathwithinthesourcefolder
lpSourceFile=fi.FullName;
gzf.LocalPath=lpSourceFile;
vpSourceFile=lpSourceFile.Replace(lpBaseFolder,string.Empty);
vpSourceFile=vpSourceFile.Replace("","/");
gzf.RelativePath=vpSourceFile;
fsIn=newFileStream(lpSourceFile,FileMode.Open,FileAccess.Read,FileShare.Read);
buffer=newbyte[fsIn.Length];
count=fsIn.Read(buffer,0,buffer.Length);
fsIn.Close();
fsIn=null;
fileModDate=fi.LastWriteTimeUtc.ToString();
gzf.ModifiedDate=fi.LastWriteTimeUtc;
gzf.Length=buffer.Length;
fileHeader=fileIndex.ToString()+","+vpSourceFile+","+fileModDate+","+buffer.Length.ToString()+"n";
header=Encoding.Default.GetBytes(fileHeader);
fsOut.Write(header,0,header.Length);
fsOut.Write(buffer,0,buffer.Length);
fsOut.Write
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 如何 压缩 文件
![提示](https://static.bdocx.com/images/bang_tan.gif)