AI编程助手
AI免费问答

.NET中的zip的压缩和解压

巴扎黑   2016-12-19 16:35   2386浏览 原创

在.net可以通过多种方式实现的zip的压缩和解压:1,使用system.io.packaging程序; 2,使用第三方类库; 3,通过system.io.compression命名空间中新增的ziparchive,的zipfile类等实现 

     一,使用system.io.packaging程序压缩和解压 

     包为一个抽象类,可用于将对象组织到定义的物理格式的单个实体中,从而实现可移植性与高效访问.zip文件是包的主物理格式。其他套餐实现可以使用其他物理格式(如xml文档,数据库或web服务。与文件系统类似,在分层组织的文件夹和文件中引用包中包含的项。虽然包装是抽象类,但package.open方法默认使用zippackage派生类。 

    system.io.packaging程序在windowsbase.dll中程序集下,使用时需要添加对windowsbase的引用。 

    1,将整个文件夹压缩成压缩 

代码///  
        ///添加一个文件夹及其子文件夹一个包沿着 
        /// 摘要> 
        /// 添加文件夹 param> 
        /// 创建包 param> 
        /// 覆盖exsisitng文件 param> 
        /// 回报> 
        静态布尔packagefolder(字符串文件夹名,字符串compressedfilename,布尔overrideexisting) 
        { 
            如果(foldername.endswith(@“”)) 
                foldername = foldername.remove(foldername.length - 1); 
            布尔结果= false; 
            如果(directory.exists(文件夹名)!) 
            { 
                返回结果; 
            } 

            (!overrideexisting && file.exists(compressedfilename))如果 
            { 
                返回结果; 
            } 
            尝试 
            { 
                使用(套餐包= package.open(compressedfilename,filemode.create)) 
                { 
                    var的filelist = directory.enumeratefiles(文件夹名,“*”,searchoption.alldirectories); 
                    的foreach(在的filelist字符串文件名) 
                    { 
                       
                        //包中的路径是所有文件夹名后的子文件夹 
                        弦pathinpackage; 
                        pathinpackage = path.getdirectoryname(文件名).replace(文件夹名,的string.empty)+“/”+ path.getfilename(文件名); 

                        乌里parturidocument = packurihelper.createparturi(新的uri(pathinpackage,urikind.relative)); 
                        的packagepart packagepartdocument = package.createpart(parturidocument,“”,compressionoption.maximum); 
                        使用(的filestream filestream =新的filestream(文件名,filemode.open,fileaccess.read)) 
                        { 
                            filestream.copyto(packagepartdocument.getstream()); 
                        } 
                    } 
                } 
                结果=真; 
            } 
            赶上(例外五) 
            { 
                抛出新的异常(“错误荏苒文件夹”文件夹名+,e); 
            } 
           
            返回结果; 
        } 
      2,将单个文件添加到压缩文件中 

的代码///  
        ///压缩文件成zip文件作为容器店 
        /// 摘要> 
        /// 文件压缩 param> 
        /// 归档文件 param> 
        /// 覆盖现有文件 param> 
        /// 回报> 
        静态布尔packagefile(字符串文件名,字符串compressedfilename,布尔overrideexisting) 
        { 
            布尔结果= false; 

            如果(file.exists(文件名)!) 
            { 
                返回结果; 
            } 

            (!overrideexisting && file.exists(compressedfilename))如果 
            { 
                返回结果; 
            } 

            尝试 
            { 
                乌里parturidocument = packurihelper.createparturi(新的uri(path.getfilename(文件名),urikind.relative)); 
               
                使用(套餐包= package.open(compressedfilename,filemode.openorcreate)) 
                { 
                    如果(package.partexists(parturidocument)) 
                    { 
                        package.deletepart(parturidocument); 
                    } 

                    的packagepart packagepartdocument = package.createpart(parturidocument,“”,compressionoption.maximum); 
                    使用(的filestream filestream =新的filestream(文件名,filemode.open,fileaccess.read)) 
                    { 
                        filestream.copyto(packagepartdocument.getstream()); 
                    } 
                } 
                结果=真; 
            } 
            赶上(例外五) 
            { 
                抛出新的异常(“错误压缩和解文件”+文件名,e); 
            } 
            
            返回结果; 
        } 3,压缩文件解压 

代码///  
        ///提取物的容器邮编。注:容器必须创建为开放式打包约定(opc)规范 
        /// 摘要> 
        /// 文件夹解压包 param> 
        /// 包文件 param> 
        /// 覆盖现有文件 param> 
        /// 回报> 
        静态布尔uncompressfile(字符串文件夹名,字符串compressedfilename,布尔overrideexisting) 
        { 
            布尔结果= false; 
            尝试 
            { 
                如果(file.exists(compressedfilename)!) 
                { 
                    返回结果; 
                } 

                的directoryinfo directoryinfo的=新directoryinfo的(文件夹名); 
                如果(!directoryinfo.exists) 
                    directoryinfo.create(); 

                使用(套餐包= package.open(compressedfilename,filemode.open,fileaccess.read)) 
                { 
                    的foreach(的packagepart的packagepart在package.getparts()) 
                    { 
                        extractpart(的packagepart,文件夹名,overrideexisting); 
                    } 
                } 

                结果=真; 
            } 
            赶上(例外五) 
            { 
                抛出新的异常(“错误解压缩文件”+ compressedfilename,e); 
            } 
            
            返回结果; 
        } 

        静态无效extractpart(的packagepart的packagepart,串targetdirectory中,布尔overrideexisting) 
        { 
            字符串stringpart = targetdirectory中+ httputility.urldecode(packagepart.uri.tostring())更换('\','/')。 

            如果(directory.exists(path.getdirectoryname(stringpart))!) 
                directory.createdirectory(path.getdirectoryname(stringpart)); 

            如果(!overrideexisting && file.exists(stringpart)) 
                回报; 
            使用(的filestream filestream =新的filestream(stringpart,filemode.create)) 
            { 
                packagepart.getstream()copyto从(filestream)。 
            } 
        } 

      使用包压缩文件会在压缩文件自动生成[内容]的.xml,用来描述压缩文件解压支持的文件格式。 

代码 
 
     
     
     
     
     
类型>同样,如果压缩文件不包含[内容]的.xml文件,或者[内容]的.xml文件不包含所对应扩展名的描述(手动添加的[内容]的.xml也是可以) ,将无法解压文件。二,使用第三方类库压缩的压缩和解压使用比较的有sharpziplib和dotnetzip。 

      1,sharpziplib,也称为“#ziplib”,基于gpl开源,支持zip,gzip在内焦油和bzip2压缩的压缩和解压缩。 

      支持.net 1.1,.net 2.0(3.5,4.0)。 

      (1)zip压缩 

codepublic静态无效的邮编(字符串srcfile,串dstfile,int缓冲区大小) 

    的filestream filestreamin =新的filestream 
(srcfile,filemode.open,fileaccess.read); 
    的filestream filestreamout =新的filestream 
(dstfile,filemode.create,fileaccess.write); 
    zipoutputstream zipoutstream =新zipoutputstream(filestreamout); 
    字节[]缓冲区=新字节; 
    入门的zipentry =新的zipentry(path.getfilename(srcfile)); 
    zipoutstream.putnextentry(输入); 
    int大小; 
    做 
    { 
        大小= filestreamin.read(缓冲,0,buffer.length); 
        zipoutstream.write(缓冲液,0,大小); 
    }而(大小> 0); 
    zipoutstream.close(); 
    filestreamout.close(); 
    filestreamin.close(); 
}(2)解压zipcodepublic静态无效的解压(字符串srcfile,串dstfile,int缓冲区大小) 

    的filestream filestreamin =新的filestream 
(srcfile,filemode.open,fileaccess.read); 
    zipinputstream zipinstream =新zipinputstream(filestreamin); 
    zipentry的条目= zipinstream.getnextentry(); 
    的filestream filestreamout =新的filestream 
(dstfile + @“”+ entry.name,filemode.create,fileaccess.write); 
    int大小; 
    字节[]缓冲区=新字节; 
    做 
    { 
        大小= zipinstream.read(缓冲,0,buffer.length); 
        filestreamout.write(缓冲液,0,大小); 
    }而(大小> 0); 
    zipinstream.close(); 
    filestreamout.close(); 
    filestreamin.close(); 
} 2,dotnetlib,是基于“ws-pl”开源,使用比较简单(1)压缩代码使用(拉链使用zipfile =新的zipfile()) 
  { 
    zip.addfile(的“readme.txt”); 
    zip.addfile(“7440-n49th.png”); 
    zip.addfile(“2008_annual_report.pdf”);        
    zip.save(“archive.zip”); 
  }(2)解压codeprivate无效myextract() 
  { 
      字符串ziptounpack =“c1p3sml.zip”; 
      字符串unpackdirectory =“解压缩文件”; 
      使用(zip1使用zipfile = zipfile.read(ziptounpack)) 
      { 
          //这里,我们提取的每个条目,但我们可以有条件地提取 
          根据条目名称,大小,日期,复选框状态等//  
          的foreach(zipentry的e在zip1) 
          { 
            e.extract(unpackdirectory,extractexistingfileaction.overwritesilently); 
          } 
       } 
    } 



    三,在.net 4.5使用ziparchive,的zipfile等类压缩和解压 

代码静态无效的主要(字串[] args) 
        { 
            字符串zippath = @“c:用户 exampleuser start.zip” 
            字符串extractpath = @“c:用户 exampleuser 提取物”; 
            字符串newfile = @“c:用户 exampleuser newfile.txt” 

            使用(ziparchive存档= zipfile.open(zippath,ziparchivemode.update)) 
            { 
                archive.createentryfromfile(newfile“newentry.txt”); 
                archive.extracttodirectory(extractpath); 
            } 
        }

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。