如何压缩多个文件.docx

上传人:b****2 文档编号:23231681 上传时间:2023-05-15 格式:DOCX 页数:24 大小:18.97KB
下载 相关 举报
如何压缩多个文件.docx_第1页
第1页 / 共24页
如何压缩多个文件.docx_第2页
第2页 / 共24页
如何压缩多个文件.docx_第3页
第3页 / 共24页
如何压缩多个文件.docx_第4页
第4页 / 共24页
如何压缩多个文件.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

如何压缩多个文件.docx

《如何压缩多个文件.docx》由会员分享,可在线阅读,更多相关《如何压缩多个文件.docx(24页珍藏版)》请在冰豆网上搜索。

如何压缩多个文件.docx

如何压缩多个文件

如何压缩多个文件/文件夹

用例:

  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

    ///

    ///Thelocationofthefilestoincludeinthezipfile,allfilesincludingfilesinsubfolderswillbeincluded.

    ///Foldertowritethezipfileinto

    ///Nameofthezipfiletowrite

    publicstaticGZipResultCompress(stringlpSourceFolder,stringlpDestFolder,stringzipFileName)

    {

      returnCompress(lpSourceFolder,"*.*",SearchOption.AllDirectories,lpDestFolder,zipFileName,true);

    }

  

    ///

    ///Compress

    ///

    ///Thelocationofthefilestoincludeinthezipfile

    ///Searchpattern(ie"*.*"or"*.txt"or"*.gif")toidendifywhatfilesinlpSourceFoldertoincludeinthezipfile

    ///OnlyfilesinlpSourceFolderorincludefilesinsubfoldersalso

    ///Foldertowritethezipfileinto

    ///Nameofthezipfiletowrite

    ///Boolean,truedeletedtheintermediatetempfile,falseleavesthetempfileinlpDestFolder(fordebugging)

    publicstaticGZipResultCompress(stringlpSourceFolder,stringsearchPattern,SearchOptionsearchOption,stringlpDestFolder,stringzipFileName,booldeleteTempFile)

    {

      DirectoryInfodi=newDirectoryInfo(lpSourceFolder);

      FileInfo[]files=di.GetFiles("*.*",searchOption);

      returnCompress(files,lpSourceFolder,lpDestFolder,zipFileName,deleteTempFile);

    }

  

    ///

    ///Compress

    ///

    ///ArrayofFileInfoobjectstobeincludedinthezipfile

    ///ArrayofFolderstring

    ///Basefoldertousewhencreatingrelativepathsforthefiles

    ///storedinthezipfile.Forexample,iflpBaseFolderis'C:

zipTestFiles',andthereisafile

    ///'C:

zipTestFilesfolder1sample.txt'inthe'files'array,therelativepathforsample.txt

    ///willbe'folder1/sample.txt'

    ///Foldertowritethezipfileinto

    ///Nameofthezipfiletowrite

    publicstaticGZipResultCompress(FileInfo[]files,string[]folders,stringlpBaseFolder,stringlpDestFolder,stringzipFileName)

    {

      //supportcompressfolder

      IListlist=newList();

      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

    ///

    ///ArrayofFileInfoobjectstobeincludedinthezipfile

    ///Basefoldertousewhencreatingrelativepathsforthefiles

    ///storedinthezipfile.Forexample,iflpBaseFolderis'C:

zipTestFiles',andthereisafile

    ///'C:

zipTestFilesfolder1sample.txt'inthe'files'array,therelativepathforsample.txt

    ///willbe'folder1/sample.txt'

    ///Foldertowritethezipfileinto

    ///Nameofthezipfiletowrite

    publicstaticGZipResultCompress(FileInfo[]files,stringlpBaseFolder,stringlpDestFolder,stringzipFileName)

    {

      returnCompress(files,lpBaseFolder,lpDestFolder,zipFileName,true);

    }

  

    ///

    ///Compress

    ///

    ///ArrayofFileInfoobjectstobeincludedinthezipfile

    ///Basefoldertousewhencreatingrelativepathsforthefiles

    ///storedinthezipfile.Forexample,iflpBaseFolderis'C:

zipTestFiles',andthereisafile

    ///'C:

zipTestFilesfolder1sample.txt'inthe'files'array,therelativepathforsample.txt

    ///willbe'folder1/sample.txt'

    ///Foldertowritethezipfileinto

    ///Nameofthezipfiletowrite

    ///Boolean,truedeletedtheintermediatetempfile,falseleavesthetempfileinlpDestFolder(fordebugging)

    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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1