![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) Chilkat Zip API ConceptsSee more Zip Examples This example demonstrates several core concepts of the The example shows how to:
The example also demonstrates the meaning of the different
This example is especially useful for understanding that methods such as Note: This example requires Chilkat v11.0.0 or greater.
IncludeFile "CkZip.pb" IncludeFile "CkZipEntry.pb" Procedure ChilkatExample() success.i = 0 zip.i = CkZip::ckCreate() If zip.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; ------------------------------------------------------------ ; This example demonstrates some of the fundamental concepts ; of the Chilkat Zip class. ; ; We will: ; ; 1) Create a new ZIP object ; 2) Add a filesystem file using AddFile ; 3) Add an in-memory text entry using AddString ; 4) Examine the ZIP entries before writing the ZIP ; 5) Write the ZIP archive ; 6) Examine how the entry types change after writing ; ; The final ZIP archive will contain: ; ; helloWorld.txt ; HelloWorld2.txt ; ; ------------------------------------------------------------ ; Initialize the Zip object. ; ; NewZip resets the Zip object to a new and empty state. ; It does NOT immediately create the .zip file. ; ; The filename passed to NewZip becomes the default filename ; used later when WriteZip or WriteZipAndClose is called. success = CkZip::ckNewZip(zip,"test.zip") If success = 0 Debug CkZip::ckLastErrorText(zip) CkZip::ckDispose(zip) ProcedureReturn EndIf Debug "ZIP filename = " + CkZip::ckFileName(zip) Debug "" ; ------------------------------------------------------------ ; Add a file from the local filesystem. ; ; AddFile does NOT immediately read or compress the file. ; Instead, it adds a reference to the filesystem file. ; ; The actual file data will be read later when WriteZip ; or WriteZipAndClose is called. ; ; Note: ; On Windows, forward slashes are equivalent to backslashes. saveExtraPath.i = 0 success = CkZip::ckAddFile(zip,"/temp/abc123/HelloWorld123.txt",saveExtraPath) If success = 0 Debug CkZip::ckLastErrorText(zip) CkZip::ckDispose(zip) ProcedureReturn EndIf ; The ZIP object now contains one entry that references ; the local filesystem file: ; ; /temp/abc123/HelloWorld123.txt ; ; ------------------------------------------------------------ ; Change the filename that will be stored in the ZIP archive. ; ; The source filesystem file remains: ; ; /temp/abc123/HelloWorld123.txt ; ; But the ZIP entry will be written as: ; ; helloWorld.txt ; entry.i = CkZipEntry::ckCreate() If entry.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkZip::ckEntryAt(zip,0,entry) CkZipEntry::setCkFileName(entry, "helloWorld.txt") ; ------------------------------------------------------------ ; Add another ZIP entry directly from in-memory text data. ; ; This entry does not reference a filesystem file. ; The text data already exists in memory. success = CkZip::ckAddString(zip,"HelloWorld2.txt","hello world!","utf-8") If success = 0 Debug CkZip::ckLastErrorText(zip) CkZip::ckDispose(zip) CkZipEntry::ckDispose(entry) ProcedureReturn EndIf ; ------------------------------------------------------------ ; Examine the ZIP entries before writing the ZIP archive. ; ; ZIP entries can have different entry types: ; ; 0 -- Mapped Entry ; An entry already existing in an open ZIP archive. ; ; 1 -- File Entry ; A reference to a filesystem file that has not yet ; been read or compressed. ; ; 2 -- Data Entry ; An in-memory entry containing text or binary data. ; ; 3 -- Null Entry ; An entry that no longer exists. ; ; 4 -- New Directory Entry ; A directory entry added by AddEmpty. ; ; At this point: ; ; helloWorld.txt => type 1 ; HelloWorld2.txt => type 2 ; Debug "Entries BEFORE writing the ZIP:" Debug "" i.i n.i = CkZip::ckNumEntries(zip) For i = 0 To n - 1 CkZip::ckEntryAt(zip,i,entry) Debug " " + CkZipEntry::ckFileName(entry) + ", type=" + Str(CkZipEntry::ckEntryType(entry)) Next Debug "" ; ------------------------------------------------------------ ; Write the ZIP archive. ; ; During this call: ; ; * Filesystem file references are read ; * Data is compressed as needed ; * The .zip file is created and written ; success = CkZip::ckWriteZip(zip) If success = 0 Debug CkZip::ckLastErrorText(zip) CkZip::ckDispose(zip) CkZipEntry::ckDispose(entry) ProcedureReturn EndIf ; ------------------------------------------------------------ ; Examine the ZIP entries again AFTER writing. ; ; Because we called WriteZip (instead of WriteZipAndClose), ; the ZIP archive remains open. ; ; The entries are automatically converted to ; "mapped entries" (type 0), meaning they now point to ; entries within the currently open ZIP archive. ; ; At this point: ; ; helloWorld.txt => type 0 ; HelloWorld2.txt => type 0 ; Debug "Entries AFTER writing the ZIP:" Debug "" n = CkZip::ckNumEntries(zip) For i = 0 To n - 1 CkZip::ckEntryAt(zip,i,entry) Debug " " + CkZipEntry::ckFileName(entry) + ", type=" + Str(CkZipEntry::ckEntryType(entry)) Next Debug "" ; ------------------------------------------------------------ ; Close the ZIP archive and clear the Zip object. CkZip::ckCloseZip(zip) Debug "Done." CkZip::ckDispose(zip) CkZipEntry::ckDispose(entry) ProcedureReturn EndProcedure |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.