Sample code for 30+ languages & platforms
PowerBuilder

Recursively Add Files to a ZIP Using AppendFilesEx

See more Zip Examples

This example demonstrates how to use the AppendFilesEx method to recursively add files from the local filesystem to a ZIP archive while controlling how directory paths and special filesystem attributes are handled.

The example:

  • Recursively scans a directory tree for files
  • Demonstrates how the saveExtraPath argument affects the paths stored within the ZIP archive
  • Includes hidden files
  • Excludes files having the Windows System attribute
  • Shows how local filesystem paths are transformed into ZIP entry paths

The AppendFilesEx method adds references to files in the local filesystem. The files are not actually read or compressed until a Write* method is called.

This method is cross-platform and works on Windows, macOS, Linux, Android, iOS, and other supported operating systems.

Some arguments are Windows-specific:

  • archiveOnly applies only to the Windows Archive attribute
  • includeSystem applies only to the Windows System attribute

On non-Windows operating systems, these Windows-specific options are simply ignored.

Suppose the local filesystem contains the following directory tree:

c:/project/files/docs/readme.txt
c:/project/files/docs/manual.pdf
c:/project/files/images/logo.png

And suppose the following call is made:

zip.AppendFilesEx("c:/project/files",_TRUE_,saveExtraPath,_FALSE_,_TRUE_,_FALSE_);

If saveExtraPath = _TRUE_, the following paths are stored in the ZIP archive:

project/files/docs/readme.txt
project/files/docs/manual.pdf
project/files/images/logo.png

In this case, the extra leading path information from the filePattern is preserved in the ZIP.

If saveExtraPath = _FALSE_, the following paths are stored in the ZIP archive instead:

docs/readme.txt
docs/manual.pdf
images/logo.png

In this case, the leading project/files portion of the path is not stored in the ZIP archive.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Zip
integer li_Recurse
integer li_SaveExtraPath
integer li_ArchiveOnly
integer li_IncludeHidden
integer li_IncludeSystem

li_Success = 0

loo_Zip = create oleobject
li_rc = loo_Zip.ConnectToNewObject("Chilkat.Zip")
if li_rc < 0 then
    destroy loo_Zip
    MessageBox("Error","Connecting to COM object failed")
    return
end if

li_Success = loo_Zip.NewZip("appendFilesEx.zip")
if li_Success = 0 then
    Write-Debug loo_Zip.LastErrorText
    destroy loo_Zip
    return
end if

// Recursively include all files beneath c:/project/files.
li_Recurse = 1

// 
// The saveExtraPath argument controls whether the extra leading path
// information from the filePattern is included in the stored ZIP paths.
// 
// For example, suppose the local filesystem contains:
// 
//     c:/project/files/docs/readme.txt
//     c:/project/files/docs/manual.pdf
//     c:/project/files/images/logo.png
// 
// And suppose AppendFilesEx is called with:
// 
//     "c:/project/files"
// 
// If saveExtraPath = 1, the ZIP stores:
// 
//     project/files/docs/readme.txt
//     project/files/docs/manual.pdf
//     project/files/images/logo.png
// 
// In this case, the extra path information from the filePattern is preserved.
// 
// ----------------------------------------------------------------
// 
// If saveExtraPath = 0, the ZIP stores paths relative to the
// directory specified by the filePattern:
// 
//     docs/readme.txt
//     docs/manual.pdf
//     images/logo.png
// 
// In this case, the leading "project/files" path is not stored in the ZIP.

// ----------------------------------------------------------------
// Preserve extra path information within the ZIP archive.
li_SaveExtraPath = 1

// Do not require the Windows archive attribute.
li_ArchiveOnly = 0

// Include hidden files.
li_IncludeHidden = 1

// Exclude files having the Windows System attribute.
li_IncludeSystem = 0

li_Success = loo_Zip.AppendFilesEx("c:/project/files",li_Recurse,li_SaveExtraPath,li_ArchiveOnly,li_IncludeHidden,li_IncludeSystem)

if li_Success = 0 then
    Write-Debug loo_Zip.LastErrorText
    destroy loo_Zip
    return
end if

// Write the ZIP archive to disk.
li_Success = loo_Zip.WriteZipAndClose()
if li_Success = 0 then
    Write-Debug loo_Zip.LastErrorText
    destroy loo_Zip
    return
end if

Write-Debug "ZIP archive created successfully."


destroy loo_Zip