Sample code for 30+ languages & platforms
PureBasic

Unzip Selected Files from a Zip Archive

See more Zip Examples

Demonstrates how to iterate over the files contained within a .zip and unzip specific files.

Chilkat PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

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

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

    success = CkZip::ckOpenZip(zip,"my_files.zip")
    If success = 0
        Debug CkZip::ckLastErrorText(zip)
        CkZip::ckDispose(zip)
        ProcedureReturn
    EndIf

    unzipDir.s = "/temp/unzipDir"

    ; Get the number of files and directories in the .zip
    n.i = CkZip::ckNumEntries(zip)

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

    i.i = 0
    While i < n

        CkZip::ckEntryAt(zip,i,entry)
        If CkZipEntry::ckIsDirectory(entry) = 0
            ; (the filename may include a path)
            Debug CkZipEntry::ckFileName(entry)

            ; Your application may choose to unzip this entry
            ; based on the filename.
            ; If the entry should be unzipped, then call Extract(unzipDir)
            success = CkZipEntry::ckExtract(entry,unzipDir)
            If success = 0
                Debug CkZipEntry::ckLastErrorText(entry)
                CkZip::ckDispose(zip)
                CkZipEntry::ckDispose(entry)
                ProcedureReturn
            EndIf

        EndIf

        i = i + 1
    Wend


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


    ProcedureReturn
EndProcedure