/d7zip

A Delphi Wrapper around 7zip.dll

Primary LanguagePascal

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Fragment
of main.html</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><meta name="generator" content="SynEdit HTML exporter" /><style type="text/css">
<!--
body { color: #000000; background-color: #FFFFFF; }
.pas1-assembler { background-color: #FFFFFF; color: #000000; }
.pas1-character { background-color: #FFFFFF; color: #000080; }
.pas1-comment { background-color: #FFFFFF; color: #000080; font-style: italic; }
.pas1-float { background-color: #FFFFFF; color: #000080; }
.pas1-hexadecimal { background-color: #FFFFFF; color: #000080; }
.pas1-identifier { background-color: #FFFFFF; color: #000000; }
.pas1-number { background-color: #FFFFFF; color: #000080; }
.pas1-preprocessor { background-color: #FFFFFF; color: #000080; font-style: italic; }
.pas1-reservedword { background-color: #FFFFFF; color: #000000; font-weight: bold; }
.pas1-space { background-color: #FFFFFF; color: #000000; }
.pas1-string { background-color: #FFFFFF; color: #000080; }
.pas1-symbol { background-color: #FFFFFF; color: #000000; }
-->
</style></head>


<body><blockquote><h1>7-zip
Delphi API</h1></blockquote>
<p>This API use the 7-zip dll (7z.dll) to read and write all 7-zip
supported archive formats.</p><p>- Autor: Henri Gourvest &lt;hgourvest@progdigy.com&gt;<br />- Licence: MPL1.1<br />- Date: 15/04/2009<br />- Version: 1.1</p>
<h2>Reading archive:</h2>
<h3>Extract to path:</h3>
<pre><code><span style="font-family: Courier New; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><span class="pas1-space"> </span><span class="pas1-reservedword">with</span><span class="pas1-space"> CreateInArchive(CLSID_CFormatZip) </span><span class="pas1-reservedword">do<br /></span><span class="pas1-space"> </span><span class="pas1-reservedword">begin<br /></span><span class="pas1-space">   OpenFile(</span><span class="pas1-string">'c:\test.zip'</span><span class="pas1-symbol">);<br />   ExtractTo(</span><span class="pas1-string">'c:\test'</span><span class="pas1-symbol">);<br /> </span><span class="pas1-reservedword">end</span><span class="pas1-symbol">;<br /></span></span><br /></code></pre><h3>Get
file list:</h3>
<pre><code><span style="font-family: Courier New; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><span class="pas1-space"> </span><span class="pas1-reservedword">with</span><span class="pas1-space"> CreateInArchive(CLSID_CFormat7z) </span><span class="pas1-reservedword">do<br /></span><span class="pas1-space"> </span><span class="pas1-reservedword">begin<br /></span><span class="pas1-space">   OpenFile(</span><span class="pas1-string">'c:\test.7z'</span><span class="pas1-symbol">);<br /></span><span class="pas1-reservedword">   for</span><span class="pas1-space"> i := </span><span class="pas1-number">0</span><span class="pas1-space"> </span><span class="pas1-reservedword">to</span><span class="pas1-space"> NumberOfItems - </span><span class="pas1-number">1</span><span class="pas1-space"> </span><span class="pas1-reservedword">do<br /></span><span class="pas1-space">   </span><span class="pas1-reservedword"> if</span><span class="pas1-space"> </span><span class="pas1-reservedword">not</span><span class="pas1-space"> ItemIsFolder[i] </span><span class="pas1-reservedword">then<br /></span><span class="pas1-space">      Writeln(ItemPath[i]);<br /></span><span class="pas1-reservedword"> end</span><span class="pas1-symbol">;<br /></span></span><br /></code></pre><h3>Extract
to stream</h3>
<pre><code><span style="font-family: Courier New; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><span class="pas1-space"> </span><span class="pas1-reservedword">with</span><span class="pas1-space"> CreateInArchive(CLSID_CFormat7z) </span><span class="pas1-reservedword">do<br /></span><span class="pas1-space"> </span><span class="pas1-reservedword">begin<br /></span><span class="pas1-space">   OpenFile(</span><span class="pas1-string">'c:\test.7z'</span><span class="pas1-symbol">);<br /></span><span class="pas1-reservedword">   for</span><span class="pas1-space"> i := </span><span class="pas1-number">0</span><span class="pas1-space"> </span><span class="pas1-reservedword">to</span><span class="pas1-space"> NumberOfItems - </span><span class="pas1-number">1</span><span class="pas1-space"> </span><span class="pas1-reservedword">do<br /></span><span class="pas1-space"> </span><span class="pas1-reservedword">    if</span><span class="pas1-space"> </span><span class="pas1-reservedword">not</span><span class="pas1-space"> ItemIsFolder[i] </span><span class="pas1-reservedword">then<br /></span><span class="pas1-space">       ExtractItem(i, stream, false);<br /></span><span class="pas1-reservedword"> end</span><span class="pas1-symbol">;<br /></span></span><br /></code></pre><h3>Extract
"n" Items</h3>
<pre><code><span style="font-family: Courier New; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><span class="pas1-reservedword">function</span><span class="pas1-space"> GetStreamCallBack(sender: Pointer; index: Cardinal;<br /></span><span class="pas1-reservedword">  var</span><span class="pas1-space"> outStream: ISequentialOutStream): HRESULT; </span><span class="pas1-reservedword">stdcall</span><span class="pas1-symbol">;<br /></span><span class="pas1-reservedword">begin<br /></span><span class="pas1-space"> </span><span class="pas1-reservedword"> case</span><span class="pas1-space"> index </span><span class="pas1-reservedword">of</span><span class="pas1-space"> ...<br />    outStream := T7zStream.Create(aStream, soReference);<br />  Result := S_OK;<br /></span><span class="pas1-reservedword">end</span><span class="pas1-symbol">;<br /><br /></span><span class="pas1-reservedword">procedure</span><span class="pas1-space"> TMainForm.ExtractClick(Sender: TObject);<br /></span><span class="pas1-reservedword">var<br /></span><span class="pas1-space">  i: integer;<br />  items: </span><span class="pas1-reservedword">array</span><span class="pas1-symbol">[</span><span class="pas1-number">0</span><span class="pas1-symbol">..</span><span class="pas1-number">2</span><span class="pas1-symbol">] </span><span class="pas1-reservedword">of</span><span class="pas1-space"> Cardinal;<br /></span><span class="pas1-reservedword">begin<br /></span><span class="pas1-space">  </span><span class="pas1-reservedword">with</span><span class="pas1-space"> CreateInArchive(CLSID_CFormat7z) </span><span class="pas1-reservedword">do<br /></span><span class="pas1-space"> </span><span class="pas1-reservedword"> begin<br /></span><span class="pas1-space">    OpenFile(</span><span class="pas1-string">'c:\test.7z'</span><span class="pas1-symbol">);<br /></span></span></code><code><span style="font-family: Courier New; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><span class="pas1-comment">    // items must be sorted by index!<br />   </span></span></code><code><span style="font-family: Courier New; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><span class="pas1-symbol">&nbsp;items[</span><span class="pas1-number">0</span><span class="pas1-symbol">] := </span><span class="pas1-number">0</span><span class="pas1-symbol">;<br />    items[</span><span class="pas1-number">1</span><span class="pas1-symbol">] := </span><span class="pas1-number">1</span><span class="pas1-symbol">;<br />    items[</span><span class="pas1-number">2</span><span class="pas1-symbol">] := </span><span class="pas1-number">2</span><span class="pas1-symbol">;<br />    ExtractItems(@items, Length(items), false, </span><span class="pas1-reservedword">nil</span><span class="pas1-symbol">, GetStreamCallBack);<br /></span><span class="pas1-reservedword">  end</span><span class="pas1-symbol">;<br /></span><span class="pas1-reservedword">end</span><span class="pas1-symbol">;<br /></span></span><br /></code></pre><h3>Open
stream</h3>
<pre><code><span style="font-family: Courier New; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><span class="pas1-space"> </span><span class="pas1-reservedword">with</span><span class="pas1-space"> CreateInArchive(CLSID_CFormatZip) </span><span class="pas1-reservedword">do<br /></span><span class="pas1-space"></span><span class="pas1-reservedword"> begin<br /></span><span class="pas1-space">   OpenStream(T7zStream.Create(TFileStream.Create(</span><span class="pas1-string">'c:\test.zip'</span><span class="pas1-symbol">, fmOpenRead), soOwned));<br />   OpenStream(aStream, soReference);<br />   ...<br /></span><span class="pas1-reservedword"> end</span><span class="pas1-symbol">;<br /></span></span><br /></code></pre><h3>Progress
bar</h3>
<pre><code><span style="font-family: Courier New; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><span class="pas1-reservedword"> function</span><span class="pas1-space"> ProgressCallback(sender: Pointer; total: boolean; value: int64): HRESULT; </span><span class="pas1-reservedword">stdcall</span><span class="pas1-symbol">;<br /></span><span class="pas1-reservedword"> begin<br /></span><span class="pas1-space">   </span><span class="pas1-reservedword">if</span><span class="pas1-space"> total </span><span class="pas1-reservedword">then<br /></span><span class="pas1-space">     Mainform.ProgressBar.Max := value </span><span class="pas1-reservedword">else<br /></span><span class="pas1-space">     Mainform.ProgressBar.Position := value;<br />   Result := S_OK;<br /></span><span class="pas1-reservedword"> end</span><span class="pas1-symbol">;<br /><br /></span><span class="pas1-reservedword"> procedure</span><span class="pas1-space"> TMainForm.ExtractClick(Sender: TObject);<br /></span><span class="pas1-reservedword"> begin<br /></span><span class="pas1-space">   </span><span class="pas1-reservedword">with</span><span class="pas1-space"> CreateInArchive(CLSID_CFormatZip) </span><span class="pas1-reservedword">do<br /></span><span class="pas1-space"> </span><span class="pas1-reservedword">  begin<br /></span><span class="pas1-space">     OpenFile(</span><span class="pas1-string">'c:\test.zip'</span><span class="pas1-symbol">);<br />     SetProgressCallback(</span><span class="pas1-reservedword">nil</span><span class="pas1-symbol">, ProgressCallback);<br />     ...<br /></span><span class="pas1-reservedword">   end</span><span class="pas1-symbol">;<br /></span><span class="pas1-reservedword"> end</span><span class="pas1-symbol">;<br /></span></span><br /></code></pre><h3>Password</h3>
<pre><code><span style="font-family: Courier New; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><span class="pas1-reservedword"> function</span><span class="pas1-space"> PasswordCallback(sender: Pointer; </span><span class="pas1-reservedword">var</span><span class="pas1-space"> password: WideString): HRESULT; </span><span class="pas1-reservedword">stdcall</span><span class="pas1-symbol">;<br /></span><span class="pas1-reservedword"> begin<br /></span><span class="pas1-space"></span></span></code><code><span style="font-family: Courier New; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><span class="pas1-comment">   // call a dialog box ...<br /></span></span></code><code><span style="font-family: Courier New; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><span class="pas1-space">&nbsp;  password := </span><span class="pas1-string">'password'</span><span class="pas1-symbol">;<br />   Result := S_OK;<br /></span><span class="pas1-reservedword"> end</span><span class="pas1-symbol">;<br /><br /></span><span class="pas1-reservedword"> procedure</span><span class="pas1-space"> TMainForm.ExtractClick(Sender: TObject);<br /></span><span class="pas1-reservedword"> begin<br /></span><span class="pas1-space">   </span><span class="pas1-reservedword">with</span><span class="pas1-space"> CreateInArchive(CLSID_CFormatZip) </span><span class="pas1-reservedword">do<br /></span><span class="pas1-space">   </span><span class="pas1-reservedword">begin<br /></span><span class="pas1-space">   </span><span class="pas1-comment">  // using callback<br /></span><span class="pas1-space">     SetPasswordCallback(</span><span class="pas1-reservedword">nil</span><span class="pas1-symbol">, PasswordCallback);<br /></span><span class="pas1-comment">     // or setting password directly<br /></span><span class="pas1-space">     SetPassword(</span><span class="pas1-string">'password'</span><span class="pas1-symbol">);<br />     OpenFile(</span><span class="pas1-string">'c:\test.zip'</span><span class="pas1-symbol">);<br />     ...<br /></span><span class="pas1-reservedword">   end</span><span class="pas1-symbol">;<br /></span><span class="pas1-reservedword"> end</span><span class="pas1-symbol">;<br /></span></span><br /></code></pre><h2>Writing
archive</h2>
<pre><code><span style="font-family: Courier New; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><span class="pas1-reservedword"> procedure</span><span class="pas1-space"> TMainForm.ExtractAllClick(Sender: TObject);<br /></span><span class="pas1-reservedword"> var<br /></span><span class="pas1-space">   Arch: I7zOutArchive;<br /></span><span class="pas1-reservedword"> begin<br /></span><span class="pas1-space">   Arch := CreateOutArchive(CLSID_CFormat7z);<br /></span><span class="pas1-comment">   // add a file<br /></span><span class="pas1-space">   Arch.AddFile(</span><span class="pas1-string">'c:\test.bin'</span><span class="pas1-symbol">, </span><span class="pas1-string">'folder\test.bin'</span><span class="pas1-symbol">);<br /></span><span class="pas1-comment">   // add files using willcards and recursive search<br /></span><span class="pas1-space">   Arch.AddFiles(</span><span class="pas1-string">'c:\test'</span><span class="pas1-symbol">, </span><span class="pas1-string">'folder'</span><span class="pas1-symbol">, </span><span class="pas1-string">'*.pas;*.dfm'</span><span class="pas1-symbol">, true);<br /></span><span class="pas1-comment">   // add a stream<br /></span><span class="pas1-space">   Arch.AddStream(aStream, soReference, faArchive, CurrentFileTime, CurrentFileTime, </span><span class="pas1-string">'folder\test.bin'</span><span class="pas1-symbol">, false, false);<br /></span><span class="pas1-comment">   // compression level<br /></span><span class="pas1-space">   SetCompressionLevel(Arch, </span><span class="pas1-number">5</span><span class="pas1-symbol">);<br /></span><span class="pas1-comment">   // compression method if &lt;&gt; LZMA<br /></span><span class="pas1-space">   SevenZipSetCompressionMethod(Arch, m7BZip2);<br /></span><span class="pas1-comment">   // add a progress bar ...<br /></span><span class="pas1-space">   Arch.SetProgressCallback(...);<br /></span><span class="pas1-comment">   // set a password if necessary<br /></span><span class="pas1-space">   Arch.SetPassword(</span><span class="pas1-string">'password'</span><span class="pas1-symbol">);<br /></span><span class="pas1-comment">   // Save to file<br /></span><span class="pas1-space">   Arch.SaveToFile(</span><span class="pas1-string">'c:\test.zip'</span><span class="pas1-symbol">);<br /></span><span class="pas1-comment">   // or a stream<br /></span><span class="pas1-space">   Arch.SaveToStream(aStream);<br /></span><span class="pas1-reservedword"> end</span><span class="pas1-symbol">;<br /></span></span>
</code></pre>
</body></html>