Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkZip.pb"
IncludeFile "CkZipEntry.pb"

Procedure ChilkatExample()

    success.i = 0

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

    zip.i = CkZip::ckCreate()
    If zip.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkZip::ckNewZip(zip,"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 0,
    ; or (2) the full absolute path, but made relative, if saveExtraPath is 1.
    ; (For example, "c:/ck2000/appData/UnitTest/qa_data/hamlet.xml" made relative is 
    ; "ck2000/appData/UnitTest/qa_data/hamlet.xml")
    saveExtraPath.i = 0
    success = CkZip::ckAddFile(zip,"c:/ck2000/appData/UnitTest/qa_data/hamlet.xml",saveExtraPath)
    If success = 0
        Debug CkZip::ckLastErrorText(zip)
        CkZip::ckDispose(zip)
        ProcedureReturn
    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".
    success = CkZip::ckAddFile(zip,"qa_data/hamlet.xml",saveExtraPath)
    If success = 0
        Debug CkZip::ckLastErrorText(zip)
        CkZip::ckDispose(zip)
        ProcedureReturn
    EndIf

    ; Examine the entries
    entry.i = CkZipEntry::ckCreate()
    If entry.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    numEntries.i = CkZip::ckNumEntries(zip)
    i.i = 0
    While i < numEntries
        CkZip::ckEntryAt(zip,i,entry)
        Debug Str(i) + ": " + CkZipEntry::ckFileName(entry)
        i = i + 1
    Wend

    ; 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"
    success = CkZip::ckWriteZip(zip)

    ; Now change the entry filepaths to whatever is desired.
    success = CkZip::ckEntryOf(zip,"hamlet.xml",entry)
    If success = 0
        Debug "entry not found"
        CkZip::ckDispose(zip)
        CkZipEntry::ckDispose(entry)
        ProcedureReturn
    EndIf

    CkZipEntry::setCkFileName(entry, "Shakespeare/Claudius1.xml")

    success = CkZip::ckEntryOf(zip,"qa_data/hamlet.xml",entry)
    If success = 0
        Debug "entry not found"
        CkZip::ckDispose(zip)
        CkZipEntry::ckDispose(entry)
        ProcedureReturn
    EndIf

    CkZipEntry::setCkFileName(entry, "Shakespeare/Claudius2.xml")

    ; Change the name of the .zip to be written and write again:
    CkZip::setCkFileName(zip, "qa_output/zipB.zip")
    success = CkZip::ckWriteZipAndClose(zip)
    If success <> 1
        Debug CkZip::ckLastErrorText(zip)
    Else
        Debug "Zip created!"
    EndIf

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


    CkZip::ckDispose(zip)
    CkZipEntry::ckDispose(entry)


    ProcedureReturn
EndProcedure