Skip to content

Commit af8733d

Browse files
committed
C#压缩
1 parent 20a44f9 commit af8733d

File tree

5 files changed

+195
-0
lines changed

5 files changed

+195
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26430.15
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GZipStreamDemo", "GZipStreamDemo\GZipStreamDemo.csproj", "{3714917F-7624-49AB-92B8-CAE8A6021FE6}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{3714917F-7624-49AB-92B8-CAE8A6021FE6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{3714917F-7624-49AB-92B8-CAE8A6021FE6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{3714917F-7624-49AB-92B8-CAE8A6021FE6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{3714917F-7624-49AB-92B8-CAE8A6021FE6}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
5+
</startup>
6+
</configuration>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{3714917F-7624-49AB-92B8-CAE8A6021FE6}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>GZipStreamDemo</RootNamespace>
10+
<AssemblyName>GZipStreamDemo</AssemblyName>
11+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="System" />
36+
<Reference Include="System.Core" />
37+
<Reference Include="System.Xml.Linq" />
38+
<Reference Include="System.Data.DataSetExtensions" />
39+
<Reference Include="Microsoft.CSharp" />
40+
<Reference Include="System.Data" />
41+
<Reference Include="System.Net.Http" />
42+
<Reference Include="System.Xml" />
43+
</ItemGroup>
44+
<ItemGroup>
45+
<Compile Include="Program.cs" />
46+
<Compile Include="Properties\AssemblyInfo.cs" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<None Include="App.config" />
50+
</ItemGroup>
51+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
52+
</Project>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.IO.Compression;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace GZipStreamDemo
10+
{
11+
/// <summary>
12+
/// 利用GZipStream来解压缩操作
13+
/// </summary>
14+
class Program
15+
{
16+
static void Main(string[] args)
17+
{
18+
Console.WriteLine("文件路径:");
19+
string path = Console.ReadLine();
20+
Console.WriteLine("1:压缩;2:解压 输入操作:");
21+
string op = Console.ReadLine();
22+
23+
FileInfo fileInfo = new FileInfo(path);
24+
if (op == "1")
25+
{
26+
CompressFile(fileInfo);
27+
}
28+
else
29+
{
30+
DecompressFile(fileInfo);
31+
}
32+
}
33+
34+
/// <summary>
35+
/// 对单个文件进行压缩
36+
/// </summary>
37+
/// <param name="fileInfo"></param>
38+
public static void CompressFile(FileInfo fileInfo)
39+
{
40+
using (FileStream inStream = fileInfo.OpenRead())
41+
{
42+
if ((File.GetAttributes(fileInfo.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden &
43+
fileInfo.Extension != ".gz")
44+
{
45+
using (FileStream outStream = File.Create(fileInfo.FullName + ".gz"))
46+
{
47+
using (GZipStream compress = new GZipStream(outStream, CompressionMode.Compress))
48+
{
49+
inStream.CopyTo(compress);
50+
Console.WriteLine("Compress {0} from {1} to {2} bytes.", fileInfo.FullName, fileInfo.Length, outStream.Length);
51+
}
52+
}
53+
}
54+
}
55+
}
56+
57+
/// <summary>
58+
/// 对单个文件进行解压缩
59+
/// </summary>
60+
/// <param name="fileInfo"></param>
61+
public static void DecompressFile(FileInfo fileInfo)
62+
{
63+
using (FileStream inStream = fileInfo.OpenRead())
64+
{
65+
string fileName = fileInfo.FullName;
66+
string originName = fileName.Substring(0, fileName.Length - fileInfo.Extension.Length);
67+
68+
using (FileStream ouStream = File.Create(originName))
69+
{
70+
using (GZipStream decompress = new GZipStream(inStream, CompressionMode.Decompress))
71+
{
72+
decompress.CopyTo(ouStream);
73+
Console.WriteLine("Decompress: {0}", fileInfo.FullName);
74+
}
75+
}
76+
}
77+
}
78+
}
79+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// 有关程序集的一般信息由以下
6+
// 控制。更改这些特性值可修改
7+
// 与程序集关联的信息。
8+
[assembly: AssemblyTitle("GZipStreamDemo")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Microsoft")]
12+
[assembly: AssemblyProduct("GZipStreamDemo")]
13+
[assembly: AssemblyCopyright("Copyright © Microsoft 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// 将 ComVisible 设置为 false 会使此程序集中的类型
18+
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
19+
//请将此类型的 ComVisible 特性设置为 true。
20+
[assembly: ComVisible(false)]
21+
22+
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23+
[assembly: Guid("3714917f-7624-49ab-92b8-cae8a6021fe6")]
24+
25+
// 程序集的版本信息由下列四个值组成:
26+
//
27+
// 主版本
28+
// 次版本
29+
// 生成号
30+
// 修订号
31+
//
32+
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
33+
// 方法是按如下所示使用“*”: :
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)