PowerBuilder
PowerBuilder
Control Stored ZIP Paths Using AppendFromDir and saveExtraPath
See more Zip Examples
This example demonstrates how the
AppendFromDir property and the
saveExtraPath argument work together to determine the
stored paths of files within a ZIP archive.
The example adds the same local filesystem file three times, but stores it in the ZIP archive using three different paths.
The local filesystem file is:
/temp/a/hamlet.xml
The resulting ZIP archive contains:
hamlet.xml
a/hamlet.xml
temp/a/hamlet.xml
The example demonstrates:
-
How
saveExtraPathcontrols whether path information is stored in the ZIP archive - How
AppendFromDirestablishes the base directory used for relative file paths - How the same filesystem file can be stored in a ZIP archive using different relative paths
- That methods such as
AddFileinitially add only references to filesystem files, and that the actual file reading and compression occur later whenWriteZipAndCloseis called
The example also illustrates an important rule:
when AppendFromDir is set, the filename passed to
AddFile should normally be a relative path rather than a
fully qualified absolute path.
Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Zip
integer li_SaveExtraPath
li_Success = 0
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
// Initialize the Zip object and set the output filename.
// The .zip file is not created until WriteZip or WriteZipAndClose is called.
li_Success = loo_Zip.NewZip("myZip.zip")
if li_Success = 0 then
Write-Debug loo_Zip.LastErrorText
destroy loo_Zip
return
end if
// This example adds the same local filesystem file three times,
// each time using a different stored path in the ZIP archive.
//
// The local file is:
//
// /temp/a/hamlet.xml
//
// The ZIP will contain:
//
// hamlet.xml
// a/hamlet.xml
// temp/a/hamlet.xml
//
// ------------------------------------------------------------
// Case 1:
// Add the file using only the filename.
//
// AddFile adds a reference to the file in the local filesystem.
// The file is not read or compressed at this point. The referenced
// file is consumed later when WriteZipAndClose is called.
li_SaveExtraPath = 0
li_Success = loo_Zip.AddFile("/temp/a/hamlet.xml",li_SaveExtraPath)
if li_Success = 0 then
Write-Debug loo_Zip.LastErrorText
destroy loo_Zip
return
end if
// Because saveExtraPath = 0, only the filename is stored
// in the ZIP archive:
//
// hamlet.xml
//
// ------------------------------------------------------------
// Case 2:
// Store the path "a/hamlet.xml" in the ZIP archive.
//
// AppendFromDir specifies the base directory for relative file paths.
// The AppendFromDir path itself is not stored in the ZIP.
//
// Because AppendFromDir is set to "/temp", the relative path
// "a/hamlet.xml" refers to the local filesystem file:
//
// /temp/a/hamlet.xml
//
// Because saveExtraPath = 1, the relative path is stored
// in the ZIP as:
//
// a/hamlet.xml
//
loo_Zip.AppendFromDir = "/temp"
li_SaveExtraPath = 1
li_Success = loo_Zip.AddFile("a/hamlet.xml",li_SaveExtraPath)
if li_Success = 0 then
Write-Debug loo_Zip.LastErrorText
destroy loo_Zip
return
end if
// ------------------------------------------------------------
// Case 3:
// Store the path "temp/a/hamlet.xml" in the ZIP archive.
//
// AppendFromDir is changed to "/" so that the relative path
// "temp/a/hamlet.xml" again refers to the same local filesystem file:
//
// /temp/a/hamlet.xml
//
// Because saveExtraPath = 1, the stored ZIP path is:
//
// temp/a/hamlet.xml
//
loo_Zip.AppendFromDir = "/"
li_SaveExtraPath = 1
li_Success = loo_Zip.AddFile("temp/a/hamlet.xml",li_SaveExtraPath)
if li_Success = 0 then
Write-Debug loo_Zip.LastErrorText
destroy loo_Zip
return
end if
// ------------------------------------------------------------
// Write the ZIP archive.
//
// This is where the referenced file is actually read from disk
// and compressed into the .zip archive.
//
// The resulting ZIP contains the same file three times,
// each with a different stored path:
//
// hamlet.xml
// a/hamlet.xml
// temp/a/hamlet.xml
//
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