C# GZip Compress DeCompress
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的更多相关文章
- AIX 文件 打包 与 压缩 tar gzip compress 的使用
今天在Aix用tar -cvf 备份,打成tar包,占有硬盘空间过大,没有压缩比, 尝试使用tar -zcvf linux系统下可以用-z 命令 (z 用gzip来压缩/解压缩文件,加上该选项后可以 ...
- Gzip模块
Gzip模块为python的压缩和解压缩模块,读写gzip 文件 一.使用gzip模块压缩文件: 1 import gzip #导入python gzip模块,注意名字为全小写 2 g = gzip. ...
- Python3 压缩与解压缩(zlib / gzip / bz2 / lzma / zipfile / tarfile)
本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog.csdn.net/Rozol/article/details/72672703 以下代码以Python3.6.1为例 L ...
- python中gzip模块的使用
gzip模块能够直接压缩和解压缩bytes-like类型的数据,同时也能实现对应格式文件的压缩与解压缩 一.数据压缩与解压缩 压缩 函数-gzip.compress(data, compresslev ...
- thttpd增加gzip压缩响应报文体功能,以减少传输数据量
thttpd thttpd是一个非常小巧的轻量级web server,它非常非常简单,仅仅提供了HTTP/1.1和简单的CGI支持,在其官方网站上有一个与其他web server(如Apache, Z ...
- 【Web优化】Yslow优化法则(四)启用Gzip压缩
Yslow的第4个经验法则指出:启用gzip压缩功能,能够降低HTTP传输的数据和时间,从而降低client请求的响应时间. 本篇是Yslow法则的第四个,主要包含三个方面的内容: 1. 什 ...
- GZIP压缩与解压
public class GZIP { /** * 字符串的压缩 * * @param str * 待压缩的字符串 * @return 返回压缩后的字符串 * @throws IOException ...
- python3 发送gzip文件请求
#condig=utf-8import requestsimport json,gzip def toGzipFormat(postData): data=bytes(json.dumps(postD ...
- 前端性能优化成神之路-HTTP压缩开启gzip
什么是HTTP压缩 HTTP压缩是指: Web服务器和浏览器之间压缩传输的”文本内容“的方法. HTTP采用通用的压缩算法,比如gzip来压缩HTML,Javascript, CSS文件. 能大大减少 ...
随机推荐
- 向C++之父Bjarne Stroustrup致敬
2013-04-25 21:30 (分类:社会人生) 非常好的文章 C ++ 的 背 影 ——C++之父Bjarne Strou ...
- Cobalt Strike生成后门
Cobalt Strike生成后门 1.Payload概念 Payload在渗透测试之中大家可以简单地理解为一段漏洞利用/植入后门的代码或程序. 2.Cobalt Strike生成后门 攻击--> ...
- ASP.NET Core ResponseCache进行缓存操作
前言 本章将介绍客户端缓存将介绍浏览器缓存和服务端缓存,使用浏览器缓存将减少对web服务器的请求次数,同时可以提升性能,避免重复的运算浪费. ASP.NET Core对于HTTP缓存分为两种: 客户端 ...
- centos7使用MySQL的Yum存储库安装mysql5.7.27
下载yum源 官网地址:http://dev.mysql.com/downloads/repo/yum/ centos7系统: http://dev.mysql.com/get/mysql57-com ...
- transient关键字和serialVersionUID
此文章很大部分转载于Java的架构师技术栈微信公众号,博主均测试通过加上自己理解写出 最近阅读java集合的源码,发现transient关键字,就了解了一下他的用法,transient关键字一般在实现 ...
- codewars--js--create phone number
Write a function that accepts an array of 10 integers (between 0 and 9), that returns a string of th ...
- 使用C#的计时器加观察者模式完成报警推送需求
前言 这两天面试了一个物联网公司高级研发,面试题是下面这样子 公司领导,部门主管,小组组长,组成员4级,假如有个 疫情预警,先通知组人员(对个人,主要有一个处理就算处理了) 如果3分钟没处理,就往组长 ...
- 「Flink」使用Managed Keyed State实现计数窗口功能
先上代码: public class WordCountKeyedState { public static void main(String[] args) throws Exception { S ...
- .net core 中如何运用 appsettings.json 进行配置开发、生产不同配置
.net core 默认会有 appsettings.Development.json 文件,这是根据ASPNETCORE_ENVIRONMENT来读取的. 新建架构appsettings.Produ ...
- 菜鸟linux
//查看系统中文件的使用情况 df -h //查看当前目录下各个文件及目录占用空间大小 du -sh *//查看当期端口使用情况netstat -tlpn //find命令详见--https://ww ...