DataFlex
DataFlex
Chilkat Zip API Concepts
See more Zip Examples
This example demonstrates several core concepts of the Chilkat.Zip class, including how ZIP entries are added, managed, and transformed as a ZIP archive progresses from an in-memory staging object to an actual written .zip file.
The example shows how to:
- Initialize a new ZIP object using
NewZip -
Add filesystem file references using
AddFile -
Add in-memory text data using
AddString - Modify the stored ZIP filename independently of the source filesystem filename
- Examine ZIP entries before and after writing the ZIP archive
- Understand how ZIP entry types change during the ZIP lifecycle
The example also demonstrates the meaning of the different ZipEntry.EntryType values:
-
1— File Entry: A reference to a filesystem file that has not yet been processed -
2— Data Entry: An in-memory entry containing text or binary data -
0— Mapped Entry: An entry already existing within the currently open ZIP archive
This example is especially useful for understanding that methods such as AddFile and AppendFiles initially add only references to filesystem files, and that the actual file reading and compression occur later when WriteZip or WriteZipAndClose is called.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoZip
Boolean iSaveExtraPath
Variant vEntry
Handle hoEntry
Integer i
Integer n
String sTemp1
Integer iTemp1
Move False To iSuccess
Get Create (RefClass(cComChilkatZip)) To hoZip
If (Not(IsComObjectCreated(hoZip))) Begin
Send CreateComObject of hoZip
End
// ------------------------------------------------------------
// 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.
Get ComNewZip Of hoZip "test.zip" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoZip To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComFileName Of hoZip To sTemp1
Showln "ZIP filename = " sTemp1
Showln ""
// ------------------------------------------------------------
// 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.
Move False To iSaveExtraPath
Get ComAddFile Of hoZip "/temp/abc123/HelloWorld123.txt" iSaveExtraPath To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoZip To sTemp1
Showln sTemp1
Procedure_Return
End
// 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
//
Get Create (RefClass(cComChilkatZipEntry)) To hoEntry
If (Not(IsComObjectCreated(hoEntry))) Begin
Send CreateComObject of hoEntry
End
Get pvComObject of hoEntry to vEntry
Get ComEntryAt Of hoZip 0 vEntry To iSuccess
Set ComFileName Of hoEntry To "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.
Get ComAddString Of hoZip "HelloWorld2.txt" "hello world!" "utf-8" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoZip To sTemp1
Showln sTemp1
Procedure_Return
End
// ------------------------------------------------------------
// 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
//
Showln "Entries BEFORE writing the ZIP:"
Showln ""
Get ComNumEntries Of hoZip To n
For i From 0 To (n - 1)
Get pvComObject of hoEntry to vEntry
Get ComEntryAt Of hoZip i vEntry To iSuccess
Get ComFileName Of hoEntry To sTemp1
Get ComEntryType Of hoEntry To iTemp1
Showln " " sTemp1 ", type=" iTemp1
Loop
Showln ""
// ------------------------------------------------------------
// Write the ZIP archive.
//
// During this call:
//
// * Filesystem file references are read
// * Data is compressed as needed
// * The .zip file is created and written
//
Get ComWriteZip Of hoZip To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoZip To sTemp1
Showln sTemp1
Procedure_Return
End
// ------------------------------------------------------------
// 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
//
Showln "Entries AFTER writing the ZIP:"
Showln ""
Get ComNumEntries Of hoZip To n
For i From 0 To (n - 1)
Get pvComObject of hoEntry to vEntry
Get ComEntryAt Of hoZip i vEntry To iSuccess
Get ComFileName Of hoEntry To sTemp1
Get ComEntryType Of hoEntry To iTemp1
Showln " " sTemp1 ", type=" iTemp1
Loop
Showln ""
// ------------------------------------------------------------
// Close the ZIP archive and clear the Zip object.
Send ComCloseZip To hoZip
Showln "Done."
End_Procedure