forked from svn2github/dotnetzip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp.htm
More file actions
312 lines (251 loc) · 9 KB
/
Copy pathCpp.htm
File metadata and controls
312 lines (251 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<html>
<head>
<title>C++/CLI</title>
<style>
p,body,a,tr,td
{ font-family: Verdana, Arial, Helvetica; font-size: 10pt }
h1,h2,h3,h4,h5,h6
{ font-family: Verdana, Arial, Helvetica; font-weight: normal; font-style: normal; }
h1 { font-size: 20pt }
h2 { font-size: 18pt; font-weight:bold; color: navy }
h3 { font-size: 16pt; font-weight:bold; color: #483d8b }
h4 { font-size: 14pt; font-weight:bold; color:#C71585; margin-bottom:2px; }
</style>
</head>
<!-- @SortOrder 80 -->
<body>
<h1>You can use DotNetZip from C++/CLI</h1>
<p>
A program written in C++/CLI can take advantage of any managed
library. It"s easy to use DotNetZip from a C++/CLI application. This
page will show some examples.
</p>
<p>
Using C++/CLI, the key difference from VB and C#, is that there is
no <strong>using</strong> statement in C++. C++ applications need to
surround the use of the ZipFile class with a try..catch.. and call the
ZipFile destructor, or call delete, in the finally clause.
</p>
<hr/>
<h3>Create a zip file</h3>
<p> This example just creates a simple zipfile. It uses the destructor.
</p>
<pre lang="C++" numberLines="true" outlining="true"
title="Create a Zip archive - destructor">
using namespace System;
using namespace Ionic::Zip;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
ZipFile ^ zip;
try
{
zip = gcnew ZipFile();
zip->AddEntry("Readme.txt", "This is the content for the Readme.txt entry.");
zip->AddFile("CreateZipFile.cpp");
zip->Save("test.zip");
}
finally
{
zip->~ZipFile();
}
Console::WriteLine(L"Press <ENTER> to quit.");
Console::ReadLine();
return 0;
}
</pre>
<p> This alternative uses the C++ deleee syntax:
</p>
<pre lang="C++" numberLines="true" outlining="true"
title="Create a Zip archive - delete">
using namespace System;
using namespace Ionic::Zip;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
ZipFile ^ zip;
try
{
zip = gcnew ZipFile();
zip->AddEntry("Readme.txt", "This is the content for the Readme.txt entry.");
zip->AddFile("CreateZipFile.cpp");
zip->Save("test.zip");
}
finally
{
delete zip;
}
Console::WriteLine(L"Press <ENTER> to quit.");
Console::ReadLine();
return 0;
}
</pre>
<h3>Building C++/CLI program that uses DotNetZip</h3>
<p>Build a C++/CLI program that uses DotNetZip, just as you would build
any C++/CLI program. </p>
<p>The easiest way is to use Visual Studio, and create a C++/CLI
project. Right click on the project and select <span style="font-family:
Courier;">References...</span>. Add a reference to the Ionic.Zip.dll
assembly. Click OK, then build your application.</p>
<p>You can also build using command-line tools. To do this, you will
need to compile using the c++ source using the cl.exe tool, specifying
the /clr option, and specifying where to find the Ionic.Zip.dll
assembly. A typical series of steps to build a simple C++/CLI program
that uses DotNetZip from one source file, supposing the name of the
source file is CreateZipFile.cpp, is: </p>
<pre>
\vc9\bin\cl.exe /Od /FD /EHa /MDd /Fo".\\" -I\vc9\include /W3 /c /Zi /clr /TP
/FU "c:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll"
/FU Ionic.Zip.dll CreateZipFile.cpp
\vc9\bin\link.exe /OUT:CreateZipFile.exe /DEBUG /ASSEMBLYDEBUG
/MANIFEST /MANIFESTFILE:"CreateZipFile.exe.intermediate.manifest"
/MANIFESTUAC:"level='asInvoker' uiAccess='false'"
/PDB:CreateZipFile.pdb /DYNAMICBASE /FIXED:No /NXCOMPAT /MACHINE:X86
/LIBPATH:\vc9\lib /LIBPATH:\winsdk\lib CreateZipFile.obj
c:\netsdk2.0\Bin\mt.exe /outputresource:"CreateZipFile.exe;#1"
/manifest CreateZipFile.exe.intermediate.manifest
</pre>
<h3>Create a zip file using AES encryption</h3>
<p> This example creates a zipfile, using AES 128-bit
encryption to encrypt the entries.
</p>
<pre lang="C++" numberLines="true" outlining="true"
title="Create an encrypted zip">
#include "stdafx.h"
using namespace System;
using namespace Ionic::Zip;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
ZipFile ^ zip;
try
{
zip = gcnew ZipFile();
zip->Password = verySecret;
zip->Encryption = EncryptionAlgorithm::WinZipAes128;
zip->AddEntry("Readme.txt", "This is the content for the Readme.txt entry.");
zip->AddFile("Data.csv");
zip->Save("test.zip");
}
finally
{
zip->~ZipFile();
}
Console::WriteLine(L"Press <ENTER> to quit.");
Console::ReadLine();
return 0;
}
</pre>
<h3>Use a SaveProgress event from C++</h3>
<p> This example creates a zipfile, and uses a SaveProgress event.
</p>
<pre lang="C++" numberLines="true" outlining="true"
title="Create an encrypted zip">
#include "stdafx.h"
using namespace System;
using namespace System::IO;
using namespace Ionic::Zip;
public ref class DnzHelloCppCli
{
private:
bool justHadByteUpdate;
public:
DnzHelloCppCli()
{
}
public:
void Run()
{
Console::WriteLine(L"Hello World");
Console::WriteLine("Using DotNetZip version {0}", ZipFile::LibraryVersion);
array<String^>^ filesToAdd = System::IO::Directory::GetFiles(".", "*.cpp");
ZipFile ^ zip;
try
{
zip = gcnew ZipFile();
zip->Password = "Harbinger";
zip->Encryption = EncryptionAlgorithm::WinZipAes128;
zip->SaveProgress += gcnew EventHandler<SaveProgressEventArgs^>(this, &DnzHelloCppCli::SaveProgress);
zip->AddEntry("Readme.txt", "This is the content for the Readme.txt entry.");
zip->AddFiles(filesToAdd, "files");
zip->Save("MyArchive.zip");
}
finally
{
zip->~ZipFile();
}
Console::WriteLine(L"Press <ENTER> to quit.");
Console::ReadLine();
return;
}
public:
void SaveProgress(Object^ sender, SaveProgressEventArgs^ e)
{
switch (e->EventType)
{
case ZipProgressEventType::Saving_Started:
{
Console::WriteLine("Saving: {0}", e->ArchiveName);
break;
}
case ZipProgressEventType::Saving_BeforeWriteEntry:
{
if (this->justHadByteUpdate)
{
Console::WriteLine();
}
Console::WriteLine(" Writing: {0} ({1}/{2})",
e->CurrentEntry->FileName,
(e->EntriesSaved + 1),
e->EntriesTotal);
this->justHadByteUpdate = false;
break;
}
case ZipProgressEventType::Saving_AfterWriteEntry:
{
if (e->CurrentEntry->InputStreamWasJitProvided)
{
e->CurrentEntry->InputStream->Close();
e->CurrentEntry->InputStream = nullptr;
}
break;
}
case ZipProgressEventType::Saving_Completed:
{
this->justHadByteUpdate = false;
Console::WriteLine();
Console::WriteLine("Done: {0}", e->ArchiveName);
break;
}
case ZipProgressEventType::Saving_EntryBytesRead:
{
if (this->justHadByteUpdate)
{
Console::SetCursorPosition(0, Console::CursorTop);
}
Console::Write(" {0}/{1} ({2:N0}%)",
e->BytesTransferred,
e->TotalBytesToTransfer,
(((double) e->BytesTransferred) / (0.01 * e->TotalBytesToTransfer)));
this->justHadByteUpdate = true;
break;
}
}
}
};
int main(array<System::String ^> ^args)
{
try
{
DnzHelloCppCli^ me = gcnew DnzHelloCppCli();
me->Run();
}
catch (Exception^ ex1)
{
Console::Error->WriteLine(String::Concat("exception: ", ex1));
}
return 0;
}
</pre>
</body>
</html>