Ejemplo n.º 1
0
 /// <summary>
 /// 解压二进制数据
 /// </summary>
 /// <param name="_from">压缩数据输入流</param>
 /// <param name="_to">解压文件输出流</param>
 /// <returns></returns>
 public static void GZipDeCompress(Stream _from, Stream _to, FileCompressProgress fcp = null)
 {
     using (var gz = new GZipInputStream(_from))
     {
         var buffer = new byte[_buffer];
         int i;
         while ((i = gz.Read(buffer, 0, buffer.Length)) > 0)
         {
             _to.Write(buffer, 0, i);
             fcp?.BeginInvoke("gz", gz.Length, gz.Position, null, null);
         }
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// 压缩二进制数据
 /// </summary>
 /// <param name="_from">原始数据输入流</param>
 /// <param name="_to">压缩文件输出流</param>
 /// <returns></returns>
 public static void GZipCompress(Stream _from, Stream _to, FileCompressProgress fcp = null)
 {
     using (var gz = new GZipOutputStream(_to))
     {
         var buffer = new byte[_buffer];
         int i;
         while ((i = _from.Read(buffer, 0, buffer.Length)) > 0)
         {
             gz.Write(buffer, 0, i);
             fcp?.BeginInvoke("gz", _from.Length, _from.Position, null, null);
         }
         gz.Flush();
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// 解包Tar文件
 /// </summary>
 /// <param name="file"></param>
 /// <param name="path"></param>
 /// <param name="fcp"></param>
 public static void TarDeCompress(string file, string path, Encoding encod, FileCompressProgress fcp = null)
 {
     if (File.Exists(file))
     {
         if (!Directory.Exists(path))
         {
             Directory.CreateDirectory(path);
         }
         using (var tar = new TarInputStream(File.OpenRead(file), encod))
         {
             TarEntry entry;
             while (null != (entry = tar.GetNextEntry()))
             {
                 if (string.Empty != entry.Name)
                 {
                     if (entry.IsDirectory)
                     {
                         Directory.CreateDirectory(Path.Combine(path, entry.Name));                   //目录
                     }
                     else
                     {
                         var name = Path.Combine(path, entry.Name);
                         using (var fs = File.OpenWrite(name))//文件
                         {
                             var buffer = new byte[_buffer];
                             int i;
                             while ((i = tar.Read(buffer, 0, buffer.Length)) > 0)
                             {
                                 fs.Write(buffer, 0, i);
                                 fcp?.BeginInvoke(name, entry.Size, fs.Position, null, null);
                             }
                         }//end open
                         new FileInfo(name).LastWriteTimeUtc = entry.ModTime;
                     }
                 }
             }
         }//end zip
     }
     else
     {
         throw new Exception("文件不存在!");
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// 解压文件
 /// </summary>
 /// <param name="file">zip文件名</param>
 /// <param name="path">要解压到的位置</param>
 /// <param name="fcp">进度事件</param>
 public static void ZipDeCompress(string file, string path, FileCompressProgress fcp = null)
 {
     if (File.Exists(file))
     {
         if (!Directory.Exists(path))
         {
             Directory.CreateDirectory(path);
         }
         using (var open = File.OpenRead(file))
             using (var zip = new ZipInputStream(open))
             {
                 ZipEntry entry;
                 while (null != (entry = zip.GetNextEntry()))
                 {
                     if (string.Empty != entry.Name)
                     {
                         if (entry.IsDirectory)
                         {
                             Directory.CreateDirectory(Path.Combine(path, entry.Name));               //目录
                         }
                         else
                         {
                             var name = Path.Combine(path, entry.Name);
                             using (var fs = File.OpenWrite(name))//文件
                             {
                                 var buffer = new byte[_buffer];
                                 int i;
                                 while ((i = zip.Read(buffer, 0, buffer.Length)) > 0)
                                 {
                                     fs.Write(buffer, 0, i);
                                     fcp?.BeginInvoke(name, entry.Size, fs.Position, null, null);
                                 }
                             }//end open
                         }
                     }
                 }
             }//end zip
     }
     else
     {
         throw new Exception("文件不存在!");
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 向压缩文件流中追加目录
 /// </summary>
 /// <param name="zip">zip流</param>
 /// <param name="dir">目录名</param>
 /// <param name="path_len">根目录长度</param>
 /// <param name="fcp">进度事件</param>
 public static void AddDirectory(this ZipOutputStream zip, string dir, int path_len, FileCompressProgress fcp = null)
 {
     if (Directory.Exists(dir))
     {
         zip.PutNextEntry(new ZipEntry(dir.Substring(path_len) + (Path.DirectorySeparatorChar == dir.Last() ? Char.MinValue : Path.DirectorySeparatorChar)));
         zip.CloseEntry();
         foreach (var item in Directory.GetFiles(dir))
         {
             zip.AddFile(item, path_len, fcp);                                          //添加文件
         }
         foreach (var item in Directory.GetDirectories(dir))
         {
             zip.AddDirectory(item, path_len, fcp);                                                //添加子目录
         }
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// 打包Tar文件
 /// </summary>
 /// <param name="names">要打包的目录或文件</param>
 /// <param name="outname">输出文件名</param>
 /// <param name="encod"></param>
 /// <param name="fcp"></param>
 public static void TarCompress(List <string> names, string outname, Encoding encod, FileCompressProgress fcp = null)
 {
     outname = outname + ".tar";                                        //拼接输出文件名
     using (var zip = new TarOutputStream(File.Create(outname), encod)) //创建输出流
     {
         foreach (var item in names)                                    //遍历
         {
             var path     = Path.GetDirectoryName(item);                //获取文件/目录所在目录
             var path_len = null == path ? 2 : path.Length;             //设置添加路径起始字符,如果为根目录则为2
             if (File.Exists(item))
             {
                 zip.AddFile(item, path_len, fcp);//添加文件
             }
             else if (Directory.Exists(item))
             {
                 zip.AddDirectory(item, path_len, fcp);//添加目录
             }
         }
         zip.Flush();
     }
 }
Ejemplo n.º 7
0
 /// <summary>
 /// 向包流中追加目录
 /// </summary>
 /// <param name="tar">zip流</param>
 /// <param name="dir">目录名</param>
 /// <param name="path_len">根目录长度</param>
 /// <param name="fcp">进度事件</param>
 public static void AddDirectory(this TarOutputStream tar, string dir, int path_len, FileCompressProgress fcp = null)
 {
     if (Directory.Exists(dir))
     {
         var head = new TarHeader();
         head.Name    = dir.Substring(path_len) + (Path.DirectorySeparatorChar == dir.Last() ? Char.MinValue : Path.DirectorySeparatorChar);
         head.ModTime = new DirectoryInfo(dir).LastWriteTimeUtc;//设置添加时间
         tar.PutNextEntry(new TarEntry(head));
         tar.CloseEntry();
         foreach (var item in Directory.GetFiles(dir))
         {
             tar.AddFile(item, path_len, fcp);                                          //添加文件
         }
         foreach (var item in Directory.GetDirectories(dir))
         {
             tar.AddDirectory(item, path_len, fcp);                                                //添加子目录
         }
     }
 }
Ejemplo n.º 8
0
 /// <summary>
 /// 向包流中追加文件
 /// </summary>
 /// <param name="tar">zip流</param>
 /// <param name="file">文件名</param>
 /// <param name="path_len">根目录长度</param>
 /// <param name="fcp">进度事件</param>
 public static void AddFile(this TarOutputStream tar, string file, int path_len, FileCompressProgress fcp = null)
 {
     if (File.Exists(file))
     {
         var head = new TarHeader();
         head.Name    = file.Substring(path_len);            //设置文件名
         head.ModTime = new FileInfo(file).LastWriteTimeUtc; //设置添加时间
         using (var fs = File.OpenRead(file))
         {
             head.Size = fs.Length;                //设置文件大小
             tar.PutNextEntry(new TarEntry(head)); //开始处理
             var buffer = new byte[_buffer];       //缓存
             int i;
             while ((i = fs.Read(buffer, 0, buffer.Length)) > 0)
             {
                 if (i < _buffer)//处理剩余字节
                 {
                     var b = new byte[i];
                     Buffer.BlockCopy(buffer, 0, b, 0, i);
                     buffer = b;
                 }
                 tar.Write(buffer, 0, buffer.Length);                        //写入流
                 fcp?.BeginInvoke(file, fs.Length, fs.Position, null, null); //通知进度
             }
             tar.CloseEntry();                                               //结束当前文件
         }
     }
 }
Ejemplo n.º 9
0
 /// <summary>
 /// 向压缩文件流中追加文件
 /// </summary>
 /// <param name="zip">zip流</param>
 /// <param name="file">文件名</param>
 /// <param name="path_len">根目录长度</param>
 /// <param name="fcp">进度事件</param>
 public static void AddFile(this ZipOutputStream zip, string file, int path_len, FileCompressProgress fcp = null)
 {
     if (File.Exists(file))
     {
         var entry = new ZipEntry(file.Substring(path_len));
         entry.DateTime = new FileInfo(file).LastWriteTimeUtc;; //设置最后修改时间
         using (var fs = File.OpenRead(file))
         {
             entry.Size = fs.Length;         //设置文件大小
             zip.PutNextEntry(entry);        //开始处理
             var crc = new Crc32();          //创建CRC校验
             crc.Reset();
             var buffer = new byte[_buffer]; //缓存
             int i;
             while ((i = fs.Read(buffer, 0, buffer.Length)) > 0)
             {
                 if (i < _buffer)//处理剩余字节
                 {
                     var b = new byte[i];
                     Buffer.BlockCopy(buffer, 0, b, 0, i);
                     buffer = b;
                 }
                 crc.Update(buffer);                                         //更新校验和
                 zip.Write(buffer, 0, buffer.Length);                        //写入流
                 fcp?.BeginInvoke(file, fs.Length, fs.Position, null, null); //通知进度
             }
             entry.Crc = crc.Value;                                          //设置校验和
             zip.CloseEntry();                                               //结束当前文件
         }
     }
 }
Ejemplo n.º 10
0
 /// <summary>
 /// 对文件或目录进行压缩
 /// </summary>
 /// <param name="names">待压缩文件或目录</param>
 /// <param name="outname">zip文件路径</param>
 /// <param name="level">压缩级别</param>
 /// <param name="fcp">压缩进度事件</param>
 public static void ZipCompress(string name, string outname, int level = Deflater.DEFLATED, FileCompressProgress fcp = null)
 {
     using (var outfs = File.Create(outname))
         using (var zip = new ZipOutputStream(outfs))       //创建输出流
         {
             zip.SetLevel(level);                           //设置压缩级别
             var path     = Path.GetDirectoryName(name);    //获取文件/目录所在目录
             var path_len = null == path ? 2 : path.Length; //设置添加路径起始字符,如果为根目录则为2
             if (File.Exists(name))
             {
                 zip.AddFile(name, path_len, fcp);//添加文件
             }
             else if (Directory.Exists(name))
             {
                 zip.AddDirectory(name, path_len, fcp);//添加目录
             }
             zip.Flush();
         }
 }
Ejemplo n.º 11
0
 /// <summary>
 /// 对文件和目录进行压缩
 /// </summary>
 /// <param name="names">待压缩文件集合及目录集合</param>
 /// <param name="outname">zip文件路径</param>
 /// <param name="level">压缩级别</param>
 /// <param name="fcp">压缩进度事件</param>
 public static void ZipCompress(List <string> names, string outname, int level = Deflater.DEFLATED, FileCompressProgress fcp = null)
 {
     outname = outname + ".zip";                                 //拼接输出文件名
     using (var zip = new ZipOutputStream(File.Create(outname))) //创建输出流
     {
         zip.SetLevel(level);                                    //设置压缩级别
         foreach (var item in names)                             //遍历
         {
             var path     = Path.GetDirectoryName(item);         //获取文件/目录所在目录
             var path_len = null == path ? 2 : path.Length;      //设置添加路径起始字符,如果为根目录则为2
             if (File.Exists(item))
             {
                 zip.AddFile(item, path_len, fcp);//添加文件
             }
             else if (Directory.Exists(item))
             {
                 zip.AddDirectory(item, path_len, fcp);//添加目录
             }
         }
         zip.Flush();
     }
 }