Skip to content

Commit f615d28

Browse files
committed
Add FastZip sample
1 parent fda9a39 commit f615d28

File tree

4 files changed

+338
-0
lines changed

4 files changed

+338
-0
lines changed

samples/cs/FastZip/AssemblyInfo.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
4+
// Information about this assembly is defined by the following
5+
// attributes.
6+
//
7+
// change them to the information which is associated with the assembly
8+
// you compile.
9+
10+
[assembly: AssemblyTitle("")]
11+
[assembly: AssemblyDescription("")]
12+
[assembly: AssemblyConfiguration("")]
13+
[assembly: AssemblyCompany("")]
14+
[assembly: AssemblyProduct("")]
15+
[assembly: AssemblyCopyright("")]
16+
[assembly: AssemblyTrademark("")]
17+
[assembly: AssemblyCulture("")]
18+
19+
// The assembly version has following format :
20+
//
21+
// Major.Minor.Build.Revision
22+
//
23+
// You can specify all values by your own or you can build default build and revision
24+
// numbers with the '*' character (the default):
25+
26+
[assembly: AssemblyVersion("1.0.*")]
27+
28+
// The following attributes specify the key for the sign of your assembly. See the
29+
// .NET Framework documentation for more information about signing.
30+
// This is not required, if you don't want signing let these attributes like they're.
31+
[assembly: AssemblyDelaySign(false)]
32+
[assembly: AssemblyKeyFile("")]

samples/cs/FastZip/FastZip.cmbx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Combine fileversion="1.0" name="FastZip" description="">
2+
<StartMode startupentry="FastZip" single="True">
3+
<Execute entry="FastZip" type="None" />
4+
<Execute entry="SharpZipLib" type="None" />
5+
</StartMode>
6+
<Entries>
7+
<Entry filename=".\FastZip.prjx" />
8+
<Entry filename="..\..\..\ICSharpCode.SharpZLib.cmbx" />
9+
</Entries>
10+
<Configurations active="Debug">
11+
<Configuration name="Release">
12+
<Entry name="FastZip" configurationname="Debug" build="False" />
13+
<Entry name="SharpZipLib" configurationname="Debug" build="False" />
14+
</Configuration>
15+
<Configuration name="Debug">
16+
<Entry name="FastZip" configurationname="Debug" build="False" />
17+
<Entry name="SharpZipLib" configurationname="Debug" build="False" />
18+
</Configuration>
19+
</Configurations>
20+
</Combine>

samples/cs/FastZip/FastZip.prjx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project name="FastZip" standardNamespace="FastZip" description="" newfilesearch="None" enableviewstate="True" version="1.1" projecttype="C#">
2+
<Contents>
3+
<File name=".\Main.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
4+
<File name=".\AssemblyInfo.cs" subtype="Code" buildaction="Compile" dependson="" data="" />
5+
</Contents>
6+
<References>
7+
<Reference type="Assembly" refto="..\..\..\bin\ICSharpCode.SharpZipLib.dll" localcopy="True" />
8+
</References>
9+
<DeploymentInformation target="" script="" strategy="File" />
10+
<Configuration runwithwarnings="True" name="Release">
11+
<CodeGeneration runtime="MsNet" compiler="Csc" compilerversion="Standard" warninglevel="4" nowarn="" includedebuginformation="False" optimize="True" unsafecodeallowed="False" generateoverflowchecks="False" mainclass="" target="Exe" definesymbols="" generatexmldocumentation="False" win32Icon="" noconfig="False" nostdlib="False" />
12+
<Execution commandlineparameters="" consolepause="True" />
13+
<Output directory=".\bin\Release" assembly="FastZip" executeScript="" executeBeforeBuild="" executeAfterBuild="" executeBeforeBuildArguments="" executeAfterBuildArguments="" />
14+
</Configuration>
15+
<Configurations active="Release">
16+
<Configuration runwithwarnings="True" name="Debug">
17+
<CodeGeneration runtime="MsNet" compiler="Csc" compilerversion="Standard" warninglevel="4" nowarn="" includedebuginformation="True" optimize="False" unsafecodeallowed="False" generateoverflowchecks="True" mainclass="" target="Exe" definesymbols="" generatexmldocumentation="False" win32Icon="" noconfig="False" nostdlib="False" />
18+
<Execution commandlineparameters="" consolepause="True" />
19+
<Output directory=".\bin\Debug" assembly="FastZip" executeScript="" executeBeforeBuild="" executeAfterBuild="" executeBeforeBuildArguments="" executeAfterBuildArguments="" />
20+
</Configuration>
21+
<Configuration runwithwarnings="True" name="Release">
22+
<CodeGeneration runtime="MsNet" compiler="Csc" compilerversion="Standard" warninglevel="4" nowarn="" includedebuginformation="False" optimize="True" unsafecodeallowed="False" generateoverflowchecks="False" mainclass="" target="Exe" definesymbols="" generatexmldocumentation="False" win32Icon="" noconfig="False" nostdlib="False" />
23+
<Execution commandlineparameters="" consolepause="True" />
24+
<Output directory=".\bin\Release" assembly="FastZip" executeScript="" executeBeforeBuild="" executeAfterBuild="" executeBeforeBuildArguments="" executeAfterBuildArguments="" />
25+
</Configuration>
26+
</Configurations>
27+
</Project>

samples/cs/FastZip/Main.cs

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
using System;
2+
using System.IO;
3+
4+
using ICSharpCode.SharpZipLib.Zip;
5+
using ICSharpCode.SharpZipLib.Core;
6+
7+
namespace Samples.FastZipDemo
8+
{
9+
class MainClass
10+
{
11+
enum Operation
12+
{
13+
Unknown,
14+
Create,
15+
Extract,
16+
List,
17+
Error
18+
};
19+
20+
void ListZipFile(string fileName, string fileFilter, string directoryFilter)
21+
{
22+
ZipFile zipFile = new ZipFile(fileName);
23+
NameFilter localFileFilter = new NameFilter(fileFilter);
24+
NameFilter localDirFilter = new NameFilter(directoryFilter);
25+
if ( zipFile.Size == 0 ) {
26+
Console.WriteLine("No entries to list");
27+
}
28+
else {
29+
for ( int i = 0 ; i < zipFile.Size; ++i)
30+
{
31+
ZipEntry e = zipFile[i];
32+
if ( e.IsFile ) {
33+
string path = Path.GetDirectoryName(e.Name);
34+
if ( localDirFilter.IsMatch(path) ) {
35+
if ( localFileFilter.IsMatch(Path.GetFileName(e.Name)) ) {
36+
Console.WriteLine(e.Name);
37+
}
38+
}
39+
}
40+
else if ( e.IsDirectory ) {
41+
if ( localDirFilter.IsMatch(e.Name) ) {
42+
Console.WriteLine(e.Name);
43+
}
44+
}
45+
else {
46+
Console.WriteLine(e.Name);
47+
}
48+
}
49+
}
50+
}
51+
52+
void ListFile(object sender, ScanEventArgs e)
53+
{
54+
Console.WriteLine("{0}", e.Name);
55+
}
56+
57+
void ListDir(object Sender, DirectoryEventArgs e)
58+
{
59+
if ( !e.HasMatchingFiles ) {
60+
Console.WriteLine("Dir:{0}", e.Name);
61+
}
62+
}
63+
64+
void ListFileSystem(string directory, bool recurse, string fileFilter, string directoryFilter)
65+
{
66+
FileSystemScanner scanner = new FileSystemScanner(fileFilter, directoryFilter);
67+
scanner.ProcessDirectory += new ProcessDirectoryDelegate(ListDir);
68+
scanner.ProcessFile += new ProcessFileDelegate(ListFile);
69+
scanner.Scan(directory, recurse);
70+
}
71+
72+
void ProcessFile(object sender, ScanEventArgs e)
73+
{
74+
Console.WriteLine(e.Name);
75+
}
76+
77+
void ProcessDirectory(object sender, DirectoryEventArgs e)
78+
{
79+
if ( !e.HasMatchingFiles ) {
80+
Console.WriteLine(e.Name);
81+
}
82+
}
83+
84+
void Run(string[] args)
85+
{
86+
bool recurse = false;
87+
string arg1 = null;
88+
string arg2 = null;
89+
string fileFilter = null;
90+
string dirFilter = null;
91+
bool verbose = false;
92+
93+
bool createEmptyDirs = false;
94+
95+
Operation op = Operation.Unknown;
96+
int argCount = 0;
97+
98+
for ( int i = 0; i < args.Length; ++i ) {
99+
if ( args[i][0] == '-' ) {
100+
string option = args[i].Substring(1).ToLower();
101+
string optArg = "";
102+
103+
int parameterIndex = option.IndexOf('=');
104+
105+
if (parameterIndex >= 0)
106+
{
107+
if (parameterIndex < option.Length - 1) {
108+
optArg = option.Substring(parameterIndex + 1);
109+
}
110+
option = option.Substring(0, parameterIndex);
111+
}
112+
113+
switch ( option ) {
114+
case "e":
115+
case "empty":
116+
createEmptyDirs = true;
117+
break;
118+
119+
case "x":
120+
case "extract":
121+
if ( op == Operation.Unknown ) {
122+
op = Operation.Extract;
123+
}
124+
else {
125+
Console.WriteLine("Only one operation at a time is permitted");
126+
op = Operation.Error;
127+
}
128+
break;
129+
130+
case "c":
131+
case "create":
132+
if ( op == Operation.Unknown ) {
133+
op = Operation.Create;
134+
}
135+
else {
136+
Console.WriteLine("Only one operation at a time is permitted");
137+
op = Operation.Error;
138+
}
139+
break;
140+
141+
case "l":
142+
case "list":
143+
if ( op == Operation.Unknown ) {
144+
op = Operation.List;
145+
}
146+
else {
147+
Console.WriteLine("Only one operation at a time is permitted");
148+
op = Operation.Error;
149+
}
150+
break;
151+
152+
153+
case "r":
154+
case "recurse":
155+
recurse = true;
156+
break;
157+
158+
case "v":
159+
case "verbose":
160+
verbose = true;
161+
break;
162+
163+
case "file":
164+
fileFilter = optArg;
165+
break;
166+
167+
case "dir":
168+
dirFilter = optArg;
169+
break;
170+
171+
default:
172+
Console.WriteLine("Unknown option {0}", args[i]);
173+
op = Operation.Error;
174+
break;
175+
}
176+
}
177+
else if ( arg1 == null ) {
178+
arg1 = args[i];
179+
++argCount;
180+
}
181+
else if ( arg2 == null ) {
182+
arg2 = args[i];
183+
++argCount;
184+
}
185+
}
186+
187+
FastZipEvents events = null;
188+
189+
if ( verbose ) {
190+
events = new FastZipEvents();
191+
events.ProcessDirectory = new ProcessDirectoryDelegate(ProcessDirectory);
192+
events.ProcessFile = new ProcessFileDelegate(ProcessFile);
193+
}
194+
195+
FastZip sz = new FastZip(events);
196+
sz.CreateEmptyDirectories = createEmptyDirs;
197+
switch ( op ) {
198+
case Operation.Create:
199+
if ( argCount == 2 ) {
200+
Console.WriteLine("Creating Zip");
201+
202+
sz.CreateZip(arg1, arg2, recurse, fileFilter);
203+
}
204+
else
205+
Console.WriteLine("Invalid arguments");
206+
break;
207+
208+
case Operation.Extract:
209+
if ( argCount == 2 ) {
210+
Console.WriteLine("Extracting Zip");
211+
sz.ExtractZip(arg1, arg2, fileFilter);
212+
}
213+
else
214+
Console.WriteLine("Invalid arguments");
215+
break;
216+
217+
case Operation.List:
218+
if ( File.Exists(arg1) ) {
219+
ListZipFile(arg1, fileFilter, dirFilter);
220+
}
221+
else if ( Directory.Exists(arg1) ) {
222+
ListFileSystem(arg1, recurse, fileFilter, dirFilter);
223+
}
224+
else {
225+
Console.WriteLine("No valid list file or directory");
226+
}
227+
break;
228+
229+
case Operation.Unknown:
230+
Console.WriteLine(
231+
"FastZip v0.1\n"
232+
+ " Usage: FastZip {options} operation args\n"
233+
+ "Operation Options: (only one permitted)\n"
234+
+ " -x zipfile targetdir : Extract files from Zip\n"
235+
+ " -c zipfile sourcedir : Create zip file\n"
236+
+ " -l zipfile|dir : List elements\n"
237+
+ "\n"
238+
+ "Behavioural options:\n"
239+
+ " -file={fileFilter}\n"
240+
+ " -dir={dirFilter}\n"
241+
+ " -e Process empty directories\n"
242+
+ " -r Recurse directories\n"
243+
+ " -v Verbose output\n"
244+
);
245+
break;
246+
247+
case Operation.Error:
248+
// Do nothing for now...
249+
break;
250+
}
251+
}
252+
253+
public static void Main(string[] args)
254+
{
255+
MainClass main = new MainClass();
256+
main.Run(args);
257+
}
258+
}
259+
}

0 commit comments

Comments
 (0)