分享

ICSharpCode.SharpZipLib生成tar、tar.gz

 Runs丶SS11 2015-09-02

原文地址:http://blog.csdn.net/nihao198503/article/details/9204115

                  http://blog.csdn.net/vividboy/article/details/2418503

源码地址:http://www./OpenSource/SharpZipLib/Default.aspx


1. 在项目中添加对ICSharpCode.SharpZipLib.dll的引用;

2. 在需要使用到ICSharpCode.SharpZipLib中定义的类的编码界面中将其导入(Imports)

(在C#中是using);

3. 在选择命名空间的时候,你会发现在有这样几个同级的命名空间:

ICSharpCode.SharpZipLib.BZip2;

ICSharpCode.SharpZipLib.GZip;

ICSharpCode.SharpZipLib.Tar;

ICSharpCode.SharpZipLib.Zip

这四个命名空间就对应着四种文件压缩方式,其中我们用的较多的是Zip的压缩方式,因为通过WinRAR软件就可以将文件压缩成.Zip的压缩包。这里有两点需要说明一下:

1) 目前还没有发现提供对.RAR方式压缩文件操作的方法;

2) BZip2, GZip 这两种压缩算法仅仅针对一个文件的压缩,如果你的压缩包要包含许多文件,并需要将这些文件解压出来进行操作的话,最好选用Tar和Zip的压缩方式。

关于这四种压缩算法的介绍可以从维基百科上得到,这里就不再赘述了。


调用方法:

  1. ///   
  2. /// 生成 ***.tar.gz 文件  
  3. ///   
  4. /// 文件基目录(源文件、生成文件所在目录)  
  5. /// 待压缩的源文件夹名  
  6. public bool CreatTarGzArchive(string strBasePath, string strSourceFolderName)  
  7. {  
  8.     if (string.IsNullOrEmpty(strBasePath)  
  9.         || string.IsNullOrEmpty(strSourceFolderName)  
  10.         || !System.IO.Directory.Exists(strBasePath)  
  11.         || !System.IO.Directory.Exists(Path.Combine(strBasePath, strSourceFolderName)))  
  12.     {  
  13.         return false;  
  14.     }  
  15.   
  16.     Environment.CurrentDirectory = strBasePath;  
  17.     string strSourceFolderAllPath = Path.Combine(strBasePath, strSourceFolderName);  
  18.     string strOupFileAllPath = Path.Combine(strBasePath, strSourceFolderName + ".tar.gz");  
  19.   
  20.     Stream outTmpStream = new FileStream(strOupFileAllPath, FileMode.OpenOrCreate);  
  21.   
  22.     //注意此处源文件大小大于4096KB  
  23.     Stream outStream = new GZipOutputStream(outTmpStream);  
  24.     TarArchive archive = TarArchive.CreateOutputTarArchive(outStream, TarBuffer.DefaultBlockFactor);  
  25.     TarEntry entry = TarEntry.CreateEntryFromFile(strSourceFolderAllPath);  
  26.     archive.WriteEntry(entry, true);  
  27.   
  28.     if (archive != null)  
  29.     {  
  30.         archive.Close();  
  31.     }  
  32.   
  33.     outTmpStream.Close();  
  34.     outStream.Close();  
  35.   
  36.     return true;  
  37. }  

 

Note:

在linux下使用gzip命令解压生成的tar.gz包会报如下错误:

gzip: dfis_bp.tar.gz: decompression OK, trailing garbage ignored

 

报这样的错通常是因为tar.gz文件的尾部有一串0x00或者0xff,这是由于很多场合下压缩算法都会在压缩完成后补充一些字节以对齐数据块。gzip在正确解压完tar.gz的内容后开始解压这样的全零填充字节就会报这样的错,并不会影响使用。


用安静模式 (gzip -q) 可以消除这些警报。


 

  1. ///   
  2. /// 生成 ***.tar 文件  
  3. ///   
  4. /// 文件基目录(源文件、生成文件所在目录)  
  5. /// 待压缩的源文件夹名  
  6. public bool CreatTarArchive(string strBasePath, string strSourceFolderName)  
  7. {  
  8.     if (string.IsNullOrEmpty(strBasePath)  
  9.         || string.IsNullOrEmpty(strSourceFolderName)  
  10.         || !System.IO.Directory.Exists(strBasePath)  
  11.         || !System.IO.Directory.Exists(Path.Combine(strBasePath, strSourceFolderName)))  
  12.     {  
  13.         return false;  
  14.     }  
  15.   
  16.     Environment.CurrentDirectory = strBasePath;  
  17.     string strSourceFolderAllPath = Path.Combine(strBasePath, strSourceFolderName);  
  18.     string strOupFileAllPath = Path.Combine(strBasePath, strSourceFolderName + ".tar");  
  19.   
  20.     Stream outStream = new FileStream(strOupFileAllPath, FileMode.OpenOrCreate);  
  21.   
  22.     TarArchive archive = TarArchive.CreateOutputTarArchive(outStream, TarBuffer.DefaultBlockFactor);  
  23.     TarEntry entry = TarEntry.CreateEntryFromFile(strSourceFolderAllPath);  
  24.     archive.WriteEntry(entry, true);  
  25.   
  26.     if (archive != null)  
  27.     {  
  28.         archive.Close();  
  29.     }  
  30.   
  31.     outStream.Close();  
  32.   
  33.     return true;  
  34. }  

 

 

  1. ///   
  2. /// tar包解压  
  3. ///   
  4. /// tar包路径  
  5. /// 解压到的目录  
  6. ///   
  7. public static bool UnpackTarFiles(string strFilePath, string strUnpackDir)  
  8. {  
  9.     try  
  10.     {  
  11.         if (!File.Exists(strFilePath))  
  12.         {  
  13.             return false;  
  14.         }  
  15.   
  16.         strUnpackDir = strUnpackDir.Replace("/""\\");  
  17.         if (!strUnpackDir.EndsWith("\\"))  
  18.         {  
  19.             strUnpackDir += "\\";  
  20.         }  
  21.   
  22.         if (!Directory.Exists(strUnpackDir))  
  23.         {  
  24.             Directory.CreateDirectory(strUnpackDir);  
  25.         }  
  26.   
  27.         FileStream fr = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);  
  28.         DhcEc.SharpZipLib.Tar.TarInputStream s = new DhcEc.SharpZipLib.Tar.TarInputStream(fr);  
  29.         DhcEc.SharpZipLib.Tar.TarEntry theEntry;  
  30.         while ((theEntry = s.GetNextEntry()) != null)  
  31.         {  
  32.             string directoryName = Path.GetDirectoryName(theEntry.Name);  
  33.             string fileName = Path.GetFileName(theEntry.Name);  
  34.   
  35.             if (directoryName != String.Empty)  
  36.                 Directory.CreateDirectory(strUnpackDir + directoryName);  
  37.   
  38.             if (fileName != String.Empty)  
  39.             {  
  40.                 FileStream streamWriter = File.Create(strUnpackDir + theEntry.Name);  
  41.   
  42.                 int size = 2048;  
  43.                 byte[] data = new byte[2048];  
  44.                 while (true)  
  45.                 {  
  46.                     size = s.Read(data, 0, data.Length);  
  47.                     if (size > 0)  
  48.                     {  
  49.                         streamWriter.Write(data, 0, size);  
  50.                     }  
  51.                     else  
  52.                     {  
  53.                         break;  
  54.                     }  
  55.                 }  
  56.   
  57.                 streamWriter.Close();  
  58.             }  
  59.         }  
  60.         s.Close();  
  61.         fr.Close();  
  62.   
  63.         return true;  
  64.     }  
  65.     catch (Exception)  
  66.     {  
  67.         return false;  
  68.     }  
  69. }   



 

  1. ///   
  2. /// zip压缩文件  
  3. ///   
  4. /// filename生成的文件的名称,如:C\123\123.zip  
  5. /// directory要压缩的文件夹路径  
  6. ///   
  7. public static bool PackFiles(string filename, string directory)  
  8. {  
  9.     try  
  10.     {  
  11.         directory = directory.Replace("/""\\");  
  12.   
  13.         if (!directory.EndsWith("\\"))  
  14.             directory += "\\";  
  15.         if (!Directory.Exists(directory))  
  16.         {  
  17.             Directory.CreateDirectory(directory);  
  18.         }  
  19.         if (File.Exists(filename))  
  20.         {  
  21.             File.Delete(filename);  
  22.         }  
  23.   
  24.         FastZip fz = new FastZip();  
  25.         fz.CreateEmptyDirectories = true;  
  26.         fz.CreateZip(filename, directory, true"");  
  27.   
  28.         return true;  
  29.     }  
  30.     catch (Exception)  
  31.     {  
  32.         return false;  
  33.     }  
  34. }  


 

  1. /// zip解压文件  
  2. ///   
  3. /// 压缩文件的名称,如:C:\123\123.zip  
  4. /// dir要解压的文件夹路径  
  5. ///   
  6. public static bool UnpackFiles(string file, string dir)  
  7. {  
  8.     try  
  9.     {  
  10.         if (!File.Exists(file))  
  11.             return false;  
  12.   
  13.         dir = dir.Replace("/""\\");  
  14.         if (!dir.EndsWith("\\"))  
  15.             dir += "\\";  
  16.   
  17.         if (!Directory.Exists(dir))  
  18.             Directory.CreateDirectory(dir);  
  19.   
  20.         FileStream fr = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);  
  21.         DhcEc.SharpZipLib.Zip.ZipInputStream s = new DhcEc.SharpZipLib.Zip.ZipInputStream(fr);  
  22.         DhcEc.SharpZipLib.Zip.ZipEntry theEntry;  
  23.         while ((theEntry = s.GetNextEntry()) != null)  
  24.         {  
  25.             string directoryName = Path.GetDirectoryName(theEntry.Name);  
  26.             string fileName = Path.GetFileName(theEntry.Name);  
  27.   
  28.             if (directoryName != String.Empty)  
  29.                 Directory.CreateDirectory(dir + directoryName);  
  30.   
  31.             if (fileName != String.Empty)  
  32.             {  
  33.                 FileStream streamWriter = File.Create(dir + theEntry.Name);  
  34.   
  35.                 int size = 2048;  
  36.                 byte[] data = new byte[2048];  
  37.                 while (true)  
  38.                 {  
  39.                     size = s.Read(data, 0, data.Length);  
  40.                     if (size > 0)  
  41.                     {  
  42.                         streamWriter.Write(data, 0, size);  
  43.                     }  
  44.                     else  
  45.                     {  
  46.                         break;  
  47.                     }  
  48.                 }  
  49.   
  50.                 streamWriter.Close();  
  51.             }  
  52.         }  
  53.         s.Close();  
  54.         fr.Close();  
  55.   
  56.         return true;  
  57.     }  
  58.     catch (Exception)  
  59.     {  
  60.         return false;  
  61.     }  
  62. }  



 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多