ICSharpCode.SharpZipLib生成tar、tar.gz
来源:互联网 发布:心蓝抢票软件下载 编辑:程序博客网 时间:2024/05/21 10:34
源码地址:http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx
调用方法:
- /// <summary>
- /// 生成 ***.tar.gz 文件
- /// </summary>
- /// <param name="strBasePath">文件基目录(源文件、生成文件所在目录)</param>
- /// <param name="strSourceFolderName">待压缩的源文件夹名</param>
- public bool CreatTarGzArchive(string strBasePath, string strSourceFolderName)
- {
- if (string.IsNullOrEmpty(strBasePath)
- || string.IsNullOrEmpty(strSourceFolderName)
- || !System.IO.Directory.Exists(strBasePath)
- || !System.IO.Directory.Exists(Path.Combine(strBasePath, strSourceFolderName)))
- {
- return false;
- }
- Environment.CurrentDirectory = strBasePath;
- string strSourceFolderAllPath = Path.Combine(strBasePath, strSourceFolderName);
- string strOupFileAllPath = Path.Combine(strBasePath, strSourceFolderName + ".tar.gz");
- Stream outTmpStream = new FileStream(strOupFileAllPath, FileMode.OpenOrCreate);
- //注意此处源文件大小大于4096KB
- Stream outStream = new GZipOutputStream(outTmpStream);
- TarArchive archive = TarArchive.CreateOutputTarArchive(outStream, TarBuffer.DefaultBlockFactor);
- TarEntry entry = TarEntry.CreateEntryFromFile(strSourceFolderAllPath);
- archive.WriteEntry(entry, true);
- if (archive != null)
- {
- archive.Close();
- }
- outTmpStream.Close();
- outStream.Close();
- return true;
- }
Note:
在linux下使用gzip命令解压生成的tar.gz包会报如下错误:
gzip: dfis_bp.tar.gz: decompression OK, trailing garbage ignored
报这样的错通常是因为tar.gz文件的尾部有一串0x00或者0xff,这是由于很多场合下压缩算法都会在压缩完成后补充一些字节以对齐数据块。gzip在正确解压完tar.gz的内容后开始解压这样的全零填充字节就会报这样的错,并不会影响使用。
用安静模式 (gzip -q) 可以消除这些警报。
- /// <summary>
- /// 生成 ***.tar 文件
- /// </summary>
- /// <param name="strBasePath">文件基目录(源文件、生成文件所在目录)</param>
- /// <param name="strSourceFolderName">待压缩的源文件夹名</param>
- public bool CreatTarArchive(string strBasePath, string strSourceFolderName)
- {
- if (string.IsNullOrEmpty(strBasePath)
- || string.IsNullOrEmpty(strSourceFolderName)
- || !System.IO.Directory.Exists(strBasePath)
- || !System.IO.Directory.Exists(Path.Combine(strBasePath, strSourceFolderName)))
- {
- return false;
- }
- Environment.CurrentDirectory = strBasePath;
- string strSourceFolderAllPath = Path.Combine(strBasePath, strSourceFolderName);
- string strOupFileAllPath = Path.Combine(strBasePath, strSourceFolderName + ".tar");
- Stream outStream = new FileStream(strOupFileAllPath, FileMode.OpenOrCreate);
- TarArchive archive = TarArchive.CreateOutputTarArchive(outStream, TarBuffer.DefaultBlockFactor);
- TarEntry entry = TarEntry.CreateEntryFromFile(strSourceFolderAllPath);
- archive.WriteEntry(entry, true);
- if (archive != null)
- {
- archive.Close();
- }
- outStream.Close();
- return true;
- }
- /// <summary>
- /// tar包解压
- /// </summary>
- /// <param name="strFilePath">tar包路径</param>
- /// <param name="strUnpackDir">解压到的目录</param>
- /// <returns></returns>
- public static bool UnpackTarFiles(string strFilePath, string strUnpackDir)
- {
- try
- {
- if (!File.Exists(strFilePath))
- {
- return false;
- }
- strUnpackDir = strUnpackDir.Replace("/", "\\");
- if (!strUnpackDir.EndsWith("\\"))
- {
- strUnpackDir += "\\";
- }
- if (!Directory.Exists(strUnpackDir))
- {
- Directory.CreateDirectory(strUnpackDir);
- }
- FileStream fr = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
- DhcEc.SharpZipLib.Tar.TarInputStream s = new DhcEc.SharpZipLib.Tar.TarInputStream(fr);
- DhcEc.SharpZipLib.Tar.TarEntry theEntry;
- while ((theEntry = s.GetNextEntry()) != null)
- {
- string directoryName = Path.GetDirectoryName(theEntry.Name);
- string fileName = Path.GetFileName(theEntry.Name);
- if (directoryName != String.Empty)
- Directory.CreateDirectory(strUnpackDir + directoryName);
- if (fileName != String.Empty)
- {
- FileStream streamWriter = File.Create(strUnpackDir + theEntry.Name);
- int size = 2048;
- byte[] data = new byte[2048];
- while (true)
- {
- size = s.Read(data, 0, data.Length);
- if (size > 0)
- {
- streamWriter.Write(data, 0, size);
- }
- else
- {
- break;
- }
- }
- streamWriter.Close();
- }
- }
- s.Close();
- fr.Close();
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
- /// <summary>
- /// zip压缩文件
- /// </summary>
- /// <param name="filename">filename生成的文件的名称,如:C\123\123.zip</param>
- /// <param name="directory">directory要压缩的文件夹路径</param>
- /// <returns></returns>
- public static bool PackFiles(string filename, string directory)
- {
- try
- {
- directory = directory.Replace("/", "\\");
- if (!directory.EndsWith("\\"))
- directory += "\\";
- if (!Directory.Exists(directory))
- {
- Directory.CreateDirectory(directory);
- }
- if (File.Exists(filename))
- {
- File.Delete(filename);
- }
- FastZip fz = new FastZip();
- fz.CreateEmptyDirectories = true;
- fz.CreateZip(filename, directory, true, "");
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
- /// <summary>
- /// zip解压文件
- /// </summary>
- /// <param name="file">压缩文件的名称,如:C:\123\123.zip</param>
- /// <param name="dir">dir要解压的文件夹路径</param>
- /// <returns></returns>
- public static bool UnpackFiles(string file, string dir)
- {
- try
- {
- if (!File.Exists(file))
- return false;
- dir = dir.Replace("/", "\\");
- if (!dir.EndsWith("\\"))
- dir += "\\";
- if (!Directory.Exists(dir))
- Directory.CreateDirectory(dir);
- FileStream fr = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
- DhcEc.SharpZipLib.Zip.ZipInputStream s = new DhcEc.SharpZipLib.Zip.ZipInputStream(fr);
- DhcEc.SharpZipLib.Zip.ZipEntry theEntry;
- while ((theEntry = s.GetNextEntry()) != null)
- {
- string directoryName = Path.GetDirectoryName(theEntry.Name);
- string fileName = Path.GetFileName(theEntry.Name);
- if (directoryName != String.Empty)
- Directory.CreateDirectory(dir + directoryName);
- if (fileName != String.Empty)
- {
- FileStream streamWriter = File.Create(dir + theEntry.Name);
- int size = 2048;
- byte[] data = new byte[2048];
- while (true)
- {
- size = s.Read(data, 0, data.Length);
- if (size > 0)
- {
- streamWriter.Write(data, 0, size);
- }
- else
- {
- break;
- }
- }
- streamWriter.Close();
- }
- }
- s.Close();
- fr.Close();
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
0 0
- ICSharpCode.SharpZipLib生成tar、tar.gz
- ICSharpCode.SharpZipLib生成tar、tar.gz
- ICSharpCode.SharpZipLib生成tar、tar.gz
- tar -xzvf .tar.gz
- tar.gz
- tar.gz
- tar.gz
- tar.gz和gz
- tar.gz tar.bz2区别
- tar.gz tar.bz2 解压缩
- tar.gz tar bz2 zip
- 使用ICSharpCode.SharpZLib,读取Tar.gz压缩包的文件名乱码问题
- 解压tar.gz,tar.bz2,tar.xz
- Ubuntu zip tar tar.gz tar.bz2
- SharpZipLib在压缩成Tar.Gz时出现的中文乱码解决方案
- lrk5.src.tar.gz
- 安装mysql tar.gz
- tar.gz文件
- android开发中常用的Eclipse快捷键
- 设置自定义servlet在spring环境下的加载顺序
- AndroidManifest.xml配置文件详解
- Linux下递归遍历目录和文件
- 导入mysql数据库中文变为?号的问题
- ICSharpCode.SharpZipLib生成tar、tar.gz
- JAVA_WEB项目之在myecplise中配置SVN插件
- db2 sequence使用
- 程序开发基础学习二(C++ Google Style 命名规则)
- 将一个工程添加到另一个工程中
- 黑马程序员-java——回调机制
- 你好宝贝
- ScrollView中嵌套ListView
- 你好宝贝