Sample code for 30+ languages & platforms
Lianja

Set Entry Filepath (in output Zip) when Zipping

Demonstrates how to set the path for the file entry within the .zip to something different than the path in the filesystem.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

// This requires the Chilkat Zip API to have been previously unlocked.
// See Unlock Chilkat Zip for sample code.

loZip = createobject("CkZip")

llSuccess = loZip.NewZip("qa_output/zipA.zip")

// When an absolute filepath is passed to AddFile, the path within the .zip
// will be one of two values:  (1) just the filename part if saveExtraPath is .F.,
// or (2) the full absolute path, but made relative, if saveExtraPath is .T..
// (For example, "c:/ck2000/appData/UnitTest/qa_data/hamlet.xml" made relative is 
// "ck2000/appData/UnitTest/qa_data/hamlet.xml")
llSaveExtraPath = .F.
llSuccess = loZip.AddFile("c:/ck2000/appData/UnitTest/qa_data/hamlet.xml",llSaveExtraPath)
if (llSuccess = .F.) then
    ? loZip.LastErrorText
    release loZip
    return
endif

// When a relative path is passed to AddFile, then saveExtraPath does not apply.
// For example, assume our current working directory is "c:/ck2000/appData/UnitTest".  The following
// call to AddFile creates an entry with the filepath "qa_data/hamlet.xml".
llSuccess = loZip.AddFile("qa_data/hamlet.xml",llSaveExtraPath)
if (llSuccess = .F.) then
    ? loZip.LastErrorText
    release loZip
    return
endif

// Examine the entries
loEntry = createobject("CkZipEntry")
lnNumEntries = loZip.NumEntries
i = 0
do while i < lnNumEntries
    loZip.EntryAt(i,loEntry)
    ? str(i) + ": " + loEntry.FileName
    i = i + 1
enddo

// The output so far is:

// 	0: hamlet.xml
// 	1: qa_data/hamlet.xml

// If we write the .zip as-is, then the zip will have two entries
// with the filepaths as shown above.

// This writes "qa_output/zipA.zip"
llSuccess = loZip.WriteZip()

// Now change the entry filepaths to whatever is desired.
llSuccess = loZip.EntryOf("hamlet.xml",loEntry)
if (llSuccess = .F.) then
    ? "entry not found"
    release loZip
    release loEntry
    return
endif

loEntry.FileName = "Shakespeare/Claudius1.xml"

llSuccess = loZip.EntryOf("qa_data/hamlet.xml",loEntry)
if (llSuccess = .F.) then
    ? "entry not found"
    release loZip
    release loEntry
    return
endif

loEntry.FileName = "Shakespeare/Claudius2.xml"

// Change the name of the .zip to be written and write again:
loZip.FileName = "qa_output/zipB.zip"
llSuccess = loZip.WriteZipAndClose()
if (llSuccess <> .T.) then
    ? loZip.LastErrorText
else
    ? "Zip created!"
endif

// Examine both zipA.zip and zipB.zip using 7zip (or another tool) to see the differences.


release loZip
release loEntry