Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(DataFlex) Chilkat Zip API ConceptsThis example helps clarify some common misconceptions w/ using the Chilkat Zip API.
Use ChilkatAx-win32.pkg Procedure Test Handle hoZip Boolean iSuccess Boolean iSaveExtraPath Variant vEntry Handle hoEntry Integer i Integer n String sTemp1 Integer iTemp1 // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. Get Create (RefClass(cComChilkatZip)) To hoZip If (Not(IsComObjectCreated(hoZip))) Begin Send CreateComObject of hoZip End // To clarify some concepts, this example will create a new .zip containing 2 files. // The NewZip method is called to initialize the zip object to a new and empty state. // It does not actually create the .zip file. It simply intializes the object. // if the zip object did not contain anything previously, then this method could be skipped // altogether. It has the side-effect of setting the zip.FileName property. Get ComNewZip Of hoZip "test.zip" To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoZip To sTemp1 Showln sTemp1 Procedure_Return End // The FileName property should now contain "test.zip". This is the name of the .zip // that is written when WriteZip or WriteZipAndClose is called. Get ComFileName Of hoZip To sTemp1 Showln "zip filename = " sTemp1 // When a file is "appended" to the zip object, we are really just appending a reference // to the file in the filesystem. A "zip entry" is added to the zip object and this entry // can be one of several types. It can point to a file. It can contain uncompressed binary // or text data, it can point to an entry in the already-opened zip (if we had called OpenZip // instead), etc. // Now.. add a reference to a single file by calling AppendOneFileOrDir // Note: On Windows, forward slashes are equivalent to backslashes Move False To iSaveExtraPath Get ComAppendOneFileOrDir Of hoZip "/temp/abc123/HelloWorld123.txt" iSaveExtraPath To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoZip To sTemp1 Showln sTemp1 Procedure_Return End // We now have a zip object with one entry, which points to the file /temp/abc123/HelloWorld123.txt // If desired, we could change the filename of the zip entry so that when the zip is written, // the file has a different name: Get ComGetEntryByIndex Of hoZip 0 To vEntry If (IsComObject(vEntry)) Begin Get Create (RefClass(cComChilkatZipEntry)) To hoEntry Set pvComObject Of hoEntry To vEntry End Set ComFileName Of hoEntry To "helloWorld.txt" Send Destroy of hoEntry // When the zip is actually written, the entry's data will stream from /temp/abc123/HelloWorld123.txt // and be compressed into an entry within the .zip having the filename "helloWorld.txt" // Now add another entry, this time from a string containing the file content: Get ComAppendString2 Of hoZip "HelloWorld2.txt" "hello world!" "utf-8" To vEntry If (IsComObject(vEntry)) Begin Get Create (RefClass(cComChilkatZipEntry)) To hoEntry Set pvComObject Of hoEntry To vEntry End Send Destroy of hoEntry // Examine the entries in the zip so far.. // Each entry can be one of the following types: // 0 -- Mapped Entry: An entry in an existing Zip file. // 1 -- File Entry: A file not yet in memory, but referenced. // These entries are added by calling AppendFiles, AppendFilesEx, AppendOneFileOrDir, etc. // 2 -- Data Entry: An entry containing uncompressed data from memory. // These entries are added by calling AppendData, AppendString, etc. // 3 -- Null Entry: An entry that no longer exists in the .zip. // 4 -- New Directory Entry: A directory entry added by calling AppendNewDir. Get ComNumEntries Of hoZip To n For i From 0 To (n - 1) Get ComGetEntryByIndex Of hoZip i To vEntry If (IsComObject(vEntry)) Begin Get Create (RefClass(cComChilkatZipEntry)) To hoEntry Set pvComObject Of hoEntry To vEntry End Get ComFileName Of hoEntry To sTemp1 Get ComEntryType Of hoEntry To iTemp1 Showln sTemp1 ", type=" iTemp1 Send Destroy of hoEntry Loop // Write the "test.zip" file. Get ComWriteZip Of hoZip To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoZip To sTemp1 Showln sTemp1 Procedure_Return End // Our zip object still contains 2 entries, but now they point to the entries within the // test.zip (We called WriteZip, which writes the .zip but does not close it. Instead, // the entries of the zip object are updated to become "mapped" entries. // For example: Get ComNumEntries Of hoZip To n For i From 0 To (n - 1) Get ComGetEntryByIndex Of hoZip i To vEntry If (IsComObject(vEntry)) Begin Get Create (RefClass(cComChilkatZipEntry)) To hoEntry Set pvComObject Of hoEntry To vEntry End Get ComFileName Of hoEntry To sTemp1 Get ComEntryType Of hoEntry To iTemp1 Showln sTemp1 ", type=" iTemp1 Send Destroy of hoEntry Loop // Finally, close the zip Send ComCloseZip To hoZip End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.