Sample code for 30+ languages & platforms
Swift

Zip Append StringBuilder

See more Zip Examples

Append the contents of a Chilkat StringBuilder object to a .zip.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let zip = CkoZip()!
    var zipPath: String? = "c:/temp/qa_output/out.zip"

    zip.newZip(zipPath: zipPath)

    let sb = CkoStringBuilder()!
    var i: Int = 0
    while i < 100 {
        sb.appendLine(str: "This is a test", crlf: true)
        i = i + 1
    }

    zip.addSb(pathInZip: "this_is_a_test.txt", sb: sb, charset: "utf-8")

    success = zip.writeAndClose()
    if success == false {
        print("\(zip.lastErrorText!)")
        return
    }

    print("Success 1.")

    // Perhaps you want to add a file to an existing .zip
    let zip2 = CkoZip()!
    // Open the .zip we just wrote..
    success = zip2.open(zipPath: zipPath)

    sb.clear()
    i = 0
    while i < 100 {
        sb.appendLine(str: "This is a test 2", crlf: true)
        i = i + 1
    }

    zip2.addSb(pathInZip: "this_is_a_test_2.txt", sb: sb, charset: "utf-8")

    var zipPath2: String? = "c:/temp/qa_output/out2.zip"
    zip2.fileName = zipPath2
    success = zip2.writeAndClose()
    if success == false {
        print("\(zip2.lastErrorText!)")
        return
    }

    print("Success 2.")

}