using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Compression;
using System.IO;
using System.Diagnostics; namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
DecompressDemo();
Console.ReadLine();
} private static void CompressDemo()
{
string source = @"D:\";
string[] files = Directory.GetFiles(source, "*", SearchOption.AllDirectories);
if(files!=null && files.Any())
{
foreach(var file in files)
{
CompressFile(file);
}
}
}
private static void CompressFile(string file)
{
Console.WriteLine($"Start to compress {file}");
Stopwatch sw = new Stopwatch();
sw.Start();
string compressedFullName = Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file)) + ".zip";
if (File.Exists(compressedFullName))
{
File.Delete(compressedFullName);
} Task compressTask = Task.Run(() =>
{
using (FileStream fs = File.Open(file, FileMode.Open))
{
using (FileStream compressedFileStream = File.Create(compressedFullName))
{
using (GZipStream gZipStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
{
fs.CopyTo(gZipStream);
}
}
}
}); compressTask.Wait();
sw.Stop();
if (compressTask.IsCompleted)
{
Console.WriteLine($"{file} has been compressed successfully,cost:{sw.ElapsedMilliseconds} ElapsedMilliseconds!");
}
}
private static void DecompressDemo()
{
string source = @"D:\";
string[] compressedFiles = Directory.GetFiles(source, "*.zip");
if (compressedFiles != null && compressedFiles.Any())
{
foreach (var file in compressedFiles)
{
DecompressFile(file);
}
}
}
private static void DecompressFile(string file)
{
Console.WriteLine($"Start to decompress {file}");
Stopwatch decompressSW = new Stopwatch();
decompressSW.Start(); string newFileName = Path.Combine(Path.GetDirectoryName(file)) + Guid.NewGuid().ToString() + Path.GetFileNameWithoutExtension(file); Task decompressTask = Task.Run(() =>
{
using (FileStream fs = File.Open(file, FileMode.Open))
{
using (FileStream decompressedStream = File.Create(newFileName))
{
using (GZipStream gzipdecompressedStream = new GZipStream(fs, CompressionMode.Decompress))
{
gzipdecompressedStream.CopyTo(decompressedStream);
}
}
}
}); decompressTask.Wait();
if(decompressTask.IsCompleted)
{
decompressSW.Stop();
Console.WriteLine($"Decompress as {newFileName} cost {decompressSW.ElapsedMilliseconds} milliseconds");
}
}
}
}

C# GZip Compress DeCompress的更多相关文章

  1. AIX 文件 打包 与 压缩 tar gzip compress 的使用

    今天在Aix用tar -cvf 备份,打成tar包,占有硬盘空间过大,没有压缩比, 尝试使用tar -zcvf  linux系统下可以用-z 命令 (z 用gzip来压缩/解压缩文件,加上该选项后可以 ...

  2. Gzip模块

    Gzip模块为python的压缩和解压缩模块,读写gzip 文件 一.使用gzip模块压缩文件: 1 import gzip #导入python gzip模块,注意名字为全小写 2 g = gzip. ...

  3. Python3 压缩与解压缩(zlib / gzip / bz2 / lzma / zipfile / tarfile)

    本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog.csdn.net/Rozol/article/details/72672703 以下代码以Python3.6.1为例 L ...

  4. python中gzip模块的使用

    gzip模块能够直接压缩和解压缩bytes-like类型的数据,同时也能实现对应格式文件的压缩与解压缩 一.数据压缩与解压缩 压缩 函数-gzip.compress(data, compresslev ...

  5. thttpd增加gzip压缩响应报文体功能,以减少传输数据量

    thttpd thttpd是一个非常小巧的轻量级web server,它非常非常简单,仅仅提供了HTTP/1.1和简单的CGI支持,在其官方网站上有一个与其他web server(如Apache, Z ...

  6. 【Web优化】Yslow优化法则(四)启用Gzip压缩

    Yslow的第4个经验法则指出:启用gzip压缩功能,能够降低HTTP传输的数据和时间,从而降低client请求的响应时间. 本篇是Yslow法则的第四个,主要包含三个方面的内容: 1.      什 ...

  7. GZIP压缩与解压

    public class GZIP { /** * 字符串的压缩 * * @param str * 待压缩的字符串 * @return 返回压缩后的字符串 * @throws IOException ...

  8. python3 发送gzip文件请求

    #condig=utf-8import requestsimport json,gzip def toGzipFormat(postData): data=bytes(json.dumps(postD ...

  9. 前端性能优化成神之路-HTTP压缩开启gzip

    什么是HTTP压缩 HTTP压缩是指: Web服务器和浏览器之间压缩传输的”文本内容“的方法. HTTP采用通用的压缩算法,比如gzip来压缩HTML,Javascript, CSS文件. 能大大减少 ...

随机推荐

  1. light oj 1014 - Ifter Party分解因子

    1014 - Ifter Party   I have an Ifter party at the 5th day of Ramadan for the contestants. For this r ...

  2. js中的innerHTML,innerText,value的区别

    首先先说一下 我自己认为的 innerHTML,innerText,value的区别 innerHTML 是在控件中加html代码 就是设置一个元素里面的HTML eg: <html> & ...

  3. 在React中使用WebUploader实现大文件分片上传的踩坑日记!

    前段时间公司项目有个大文件分片上传的需求,项目是用React写的,大文件分片上传这个功能使用了WebUploader这个组件. 具体交互是: 1. 点击上传文件button后出现弹窗,弹窗内有选择文件 ...

  4. StarUML之二、StarUML初识

    为什么用StarUML UML建模工具比较常见的PowerDesigner ROSE StarUML starUML-开源免费(1-2百M),PowerDesigner-精细和一体化(6-7百M),R ...

  5. MySQL导出数据时提示文件损坏

    使用Navicat工具,优先将整个数据库的表和数据导出. 如果遇到文件损坏错误可以在表实例界面选中所有表,然后将表转储为SQL文件(结构和数据). 在目标数据库执行导出的SQL文件,导入结构和数据. ...

  6. AB实验人群定向HTE模型5 - Meta Learner

    Meta Learner和之前介绍的Casual Tree直接估计模型不同,属于间接估计模型的一种.它并不直接对treatment effect进行建模,而是通过对response effect(ta ...

  7. MFC/QT 学习笔记(三)——MFC模板创建

    新建项目->MFC模板->MFC应用程序->应用程序类型:单个文档:项目样式:MFC 标准->下一步...OK 此时点击运行,可直接弹出窗口. 调整 视图->类视图: · ...

  8. springCloud进阶(微服务架构&Eureka)

    springCloud进阶(微服务架构&Eureka) 1. 微服务集群 1.1 为什么要集群 为了提供并发量,有时同一个服务提供者可以部署多个(商品服务).这个客户端在调用时要根据一定的负责 ...

  9. java 关于xlsx(xls) 和 csv 文件的数据解析

    1.适用于xlsx 和 xls  <!--xlsx和xls文件pom依赖--> <dependency> <groupId>org.apache.poi</g ...

  10. vue及vant框架,多语言配置

    1.安装 vue-i18n,( cnpm install vue-i18n --save ) 2.在入口,main.js 中引入 (import Vuei18n from "vue-i18n ...