Skip to content

Commit 53d60da

Browse files
committed
Refactoring Samples to Usable Utilities
* renamed minibzip2 to Cmd_BZip2 * renamed minigzip to Cmd_GZip * renamed zipfiletest to Cmd_ZipInfo
1 parent e462f14 commit 53d60da

14 files changed

Lines changed: 683 additions & 450 deletions

File tree

ICSharpCode.SharpZipLib.Samples/Samples.sln

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CreateZipFile", "cs\CreateZ
3737
EndProject
3838
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FastZip", "cs\FastZip\FastZip.csproj", "{61BE8DBC-149A-4564-B891-9EEA4A09ED12}"
3939
EndProject
40-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "minibzip2", "cs\minibzip2\minibzip2.csproj", "{7520CE0B-F1D4-4148-8427-BB03669D7672}"
40+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cmd_BZip2", "cs\minibzip2\Cmd_BZip2.csproj", "{7520CE0B-F1D4-4148-8427-BB03669D7672}"
4141
EndProject
42-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "minigzip", "cs\minigzip\minigzip.csproj", "{301EF496-ED72-4448-A98E-A60D6BF6E0AE}"
42+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cmd_GZip", "cs\minigzip\Cmd_GZip.csproj", "{301EF496-ED72-4448-A98E-A60D6BF6E0AE}"
4343
EndProject
4444
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "sz", "cs\sz\sz.csproj", "{CE0CEA62-CC91-4D17-BC57-A5FCACCA6A1F}"
4545
EndProject
4646
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tar", "cs\tar\Tar.csproj", "{12C2AD0C-B815-426D-AFF1-7787C2DCA0F0}"
4747
EndProject
4848
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "zf", "cs\zf\zf.csproj", "{90E83588-261C-45A3-964F-4B707357D437}"
4949
EndProject
50-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "zipfiletest", "cs\zipfiletest\zipfiletest.csproj", "{4C2CF0B1-FA7B-4FB6-83BA-24A9549AAB02}"
50+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cmd_ZipInfo", "cs\zipfiletest\Cmd_ZipInfo.csproj", "{4C2CF0B1-FA7B-4FB6-83BA-24A9549AAB02}"
5151
EndProject
5252
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "unzipfile", "cs\unzipfile\unzipfile.csproj", "{DE6AA83B-1EFA-41B0-85D9-49DA1B0202BC}"
5353
EndProject
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
using System.Reflection;
2-
using System.Runtime.CompilerServices;
32

4-
[assembly: AssemblyTitle("SharpDevelop")]
5-
[assembly: AssemblyDescription("free C# IDE")]
6-
[assembly: AssemblyConfiguration("")]
7-
[assembly: AssemblyCompany("")]
8-
[assembly: AssemblyProduct("SharpDevelop")]
9-
[assembly: AssemblyCopyright("Copyright © 2000-2016 AlphaSierraPapa for the SharpZipLib Team")]
10-
[assembly: AssemblyTrademark("")]
3+
[assembly: AssemblyTitle("Cmd_BZip2")]
4+
[assembly: AssemblyDescription("bzip2 based file compression")]
115
[assembly: AssemblyCulture("")]
126

13-
[assembly: AssemblyVersion("0.85.4.369")]
14-
157
[assembly: AssemblyDelaySign(false)]
168
[assembly: AssemblyKeyFile("")]
179
[assembly: AssemblyKeyName("")]
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
// SharpZipLib samples
2+
// Copyright © 2000-2016 AlphaSierraPapa for the SharpZipLib Team
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without modification, are
6+
// permitted provided that the following conditions are met:
7+
//
8+
// - Redistributions of source code must retain the above copyright notice, this list
9+
// of conditions and the following disclaimer.
10+
//
11+
// - Redistributions in binary form must reproduce the above copyright notice, this list
12+
// of conditions and the following disclaimer in the documentation and/or other materials
13+
// provided with the distribution.
14+
//
15+
// - Neither the name of the SharpDevelop team nor the names of its contributors may be used to
16+
// endorse or promote products derived from this software without specific prior written
17+
// permission.
18+
//
19+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &AS IS& AND ANY EXPRESS
20+
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21+
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22+
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25+
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26+
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
28+
using System;
29+
using System.IO;
30+
31+
using ICSharpCode.SharpZipLib.BZip2;
32+
33+
class Cmd_BZip2
34+
{
35+
static void ShowHelp()
36+
{
37+
Console.Error.WriteLine("bzip2, a block-sorting file compressor.");
38+
Console.Error.WriteLine("Version {0} using SharpZipLib {1}",
39+
typeof(Cmd_BZip2).Assembly.GetName().Version,
40+
typeof(BZip2).Assembly.GetName().Version);
41+
Console.Error.WriteLine("\n usage: {0} [flags and input files in any order]\n",
42+
// Environment.GetCommandLineArgs()[0]
43+
System.AppDomain.CurrentDomain.FriendlyName);
44+
Console.Error.WriteLine("");
45+
Console.Error.WriteLine(" -h --help print this message");
46+
Console.Error.WriteLine(" -d --decompress force decompression");
47+
Console.Error.WriteLine(" -z --compress force compression");
48+
Console.Error.WriteLine(" -1 .. -9 set block size to 100k .. 900k");
49+
Console.Error.WriteLine(" --fast alias for -1");
50+
Console.Error.WriteLine(" --best alias for -9");
51+
}
52+
53+
#region Command parsing
54+
enum Command
55+
{
56+
Nothing,
57+
Help,
58+
Compress,
59+
Decompress,
60+
Stop,
61+
}
62+
63+
class ArgumentParser
64+
{
65+
public ArgumentParser(string[] args)
66+
{
67+
if (System.AppDomain.CurrentDomain.FriendlyName.Contains("bzip2")) {
68+
SetCommand(Command.Compress);
69+
} else if (System.AppDomain.CurrentDomain.FriendlyName.Contains("bunzip2")) {
70+
SetCommand(Command.Decompress);
71+
}
72+
73+
foreach (string argument in args) {
74+
switch (argument) {
75+
case "-?": // for backwards compatibility
76+
case "-h":
77+
case "--help":
78+
SetCommand(Command.Help);
79+
break;
80+
case "-d":
81+
case "--decompress":
82+
SetCommand(Command.Decompress);
83+
break;
84+
case "-c": // for backwards compatibility
85+
case "-z":
86+
case "--compress":
87+
SetCommand(Command.Compress);
88+
break;
89+
case "-1":
90+
case "-2":
91+
case "-3":
92+
case "-4":
93+
case "-5":
94+
case "-6":
95+
case "-7":
96+
case "-8":
97+
case "-9":
98+
SetLevel((int)argument[1] - 48);
99+
break;
100+
case "--fast":
101+
SetLevel(1);
102+
break;
103+
case "--best":
104+
SetLevel(9);
105+
break;
106+
default:
107+
if (argument[0] == '-') {
108+
Console.Error.WriteLine("Unknown argument {0}", argument);
109+
command_ = Command.Stop;
110+
} else if (file_ == null) {
111+
file_ = argument;
112+
113+
if (!System.IO.File.Exists(file_)) {
114+
Console.Error.WriteLine("File not found '{0}'", file_);
115+
command_ = Command.Stop;
116+
}
117+
} else {
118+
Console.Error.WriteLine("File has already been specified");
119+
command_ = Command.Stop;
120+
}
121+
break;
122+
}
123+
}
124+
125+
if (command_ == Command.Nothing) {
126+
if (file_ == null) {
127+
command_ = Command.Help;
128+
} else {
129+
command_ = Command.Compress;
130+
}
131+
}
132+
}
133+
134+
void SetCommand(Command command)
135+
{
136+
if ((command_ != Command.Nothing) && (command_ != Command.Stop)) {
137+
Console.Error.WriteLine("Command already specified");
138+
command_ = Command.Stop;
139+
} else {
140+
command_ = command;
141+
}
142+
}
143+
144+
void SetLevel(int level)
145+
{
146+
if (level_ != 0) {
147+
Console.Error.WriteLine("Level already specified");
148+
level_ = 0;
149+
} else {
150+
level_ = level;
151+
}
152+
}
153+
154+
public string Source {
155+
get { return file_; }
156+
}
157+
158+
public string Target {
159+
get {
160+
string result;
161+
if (command_ == Command.Compress) {
162+
result = file_ + ".bz";
163+
} else {
164+
result = Path.GetFileNameWithoutExtension(file_);
165+
}
166+
return result;
167+
}
168+
}
169+
170+
public Command Command {
171+
get { return command_; }
172+
}
173+
174+
public int Level {
175+
get { return level_; }
176+
}
177+
178+
#region Instance Fields
179+
Command command_ = Command.Nothing;
180+
string file_;
181+
int level_;
182+
#endregion
183+
}
184+
#endregion
185+
186+
public static int Main(string[] args)
187+
{
188+
if (args.Length == 0) {
189+
ShowHelp();
190+
return 1;
191+
}
192+
193+
var parser = new ArgumentParser(args);
194+
195+
switch (parser.Command) {
196+
case Command.Help:
197+
ShowHelp();
198+
break;
199+
200+
case Command.Compress:
201+
Console.WriteLine("Compressing {0} to {1} at level {2}", parser.Source, parser.Target, parser.Level);
202+
BZip2.Compress(File.OpenRead(parser.Source), File.Create(parser.Target), true, parser.Level);
203+
break;
204+
205+
case Command.Decompress:
206+
Console.WriteLine("Decompressing {0} to {1}", parser.Source, parser.Target);
207+
BZip2.Decompress(File.OpenRead(parser.Source), File.Create(parser.Target), true);
208+
break;
209+
}
210+
211+
return 0;
212+
}
213+
}

ICSharpCode.SharpZipLib.Samples/cs/minibzip2/minibzip2.csproj renamed to ICSharpCode.SharpZipLib.Samples/cs/minibzip2/Cmd_BZip2.csproj

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@
55
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
66
<SchemaVersion>2.0</SchemaVersion>
77
<ProjectGuid>{7520CE0B-F1D4-4148-8427-BB03669D7672}</ProjectGuid>
8-
<RootNamespace>NewProject</RootNamespace>
9-
<AssemblyName>minibzip</AssemblyName>
8+
<RootNamespace>ICSharpCode.SharpZipLib.Samples</RootNamespace>
9+
<AssemblyName>Cmd_BZip2</AssemblyName>
1010
<OutputType>Exe</OutputType>
1111
<ApplicationIcon />
1212
<WarningLevel>4</WarningLevel>
13-
<StartupObject />
13+
<StartupObject>Cmd_BZip2</StartupObject>
1414
<NoStdLib>False</NoStdLib>
1515
<NoConfig>False</NoConfig>
1616
<RunPostBuildEvent>OnSuccessfulBuild</RunPostBuildEvent>
1717
<PreBuildEvent />
18-
<PostBuildEvent />
18+
<PostBuildEvent>copy Cmd_BZip2.exe bzip2.exe
19+
copy Cmd_BZip2.exe bunzip2.exe</PostBuildEvent>
1920
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
2021
<FileUpgradeFlags>
2122
</FileUpgradeFlags>
2223
<UpgradeBackupLocation>
2324
</UpgradeBackupLocation>
2425
<OldToolsVersion>2.0</OldToolsVersion>
2526
<TargetFrameworkProfile />
27+
<IsWebBootstrapper>false</IsWebBootstrapper>
2628
<PublishUrl>publish\</PublishUrl>
2729
<Install>true</Install>
2830
<InstallFrom>Disk</InstallFrom>
@@ -35,7 +37,6 @@
3537
<MapFileExtensions>true</MapFileExtensions>
3638
<ApplicationRevision>0</ApplicationRevision>
3739
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
38-
<IsWebBootstrapper>false</IsWebBootstrapper>
3940
<UseApplicationTrust>false</UseApplicationTrust>
4041
<BootstrapperEnabled>true</BootstrapperEnabled>
4142
</PropertyGroup>
@@ -44,22 +45,30 @@
4445
<Optimize>false</Optimize>
4546
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
4647
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
47-
<OutputPath>bin\Debug\</OutputPath>
48+
<OutputPath>bin\</OutputPath>
4849
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
4950
<DebugType>Full</DebugType>
5051
<Prefer32Bit>false</Prefer32Bit>
5152
<DefineConstants>TRACE;DEBUG</DefineConstants>
53+
<ErrorReport>prompt</ErrorReport>
54+
<RunCodeAnalysis>true</RunCodeAnalysis>
55+
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
56+
<FileAlignment>2048</FileAlignment>
5257
</PropertyGroup>
5358
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
5459
<DebugSymbols>True</DebugSymbols>
5560
<Optimize>True</Optimize>
5661
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
5762
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
58-
<DefineConstants />
59-
<OutputPath>bin\Release\</OutputPath>
63+
<DefineConstants>TRACE</DefineConstants>
64+
<OutputPath>..\bin\</OutputPath>
6065
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
6166
<Prefer32Bit>false</Prefer32Bit>
62-
<DocumentationFile>bin\Release\minibzip.XML</DocumentationFile>
67+
<DocumentationFile>..\bin\Cmd_BZip2.XML</DocumentationFile>
68+
<ErrorReport>prompt</ErrorReport>
69+
<DebugType>pdbonly</DebugType>
70+
<RunCodeAnalysis>true</RunCodeAnalysis>
71+
<FileAlignment>2048</FileAlignment>
6372
</PropertyGroup>
6473
<PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' ">
6574
<RegisterForComInterop>False</RegisterForComInterop>
@@ -76,7 +85,10 @@
7685
</Reference>
7786
</ItemGroup>
7887
<ItemGroup>
79-
<Compile Include="Main.cs" />
88+
<Compile Include="..\..\..\GlobalAssemblyInfo.cs">
89+
<Link>GlobalAssemblyInfo.cs</Link>
90+
</Compile>
91+
<Compile Include="Cmd_BZip2.cs" />
8092
<Compile Include="AssemblyInfo.cs" />
8193
</ItemGroup>
8294
<ItemGroup>

0 commit comments

Comments
 (0)