Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

Get Compressed ZIP Entry Data as Base64 Using ZipEntry.CopyToBase64

See more Zip Examples

This example creates a small ZIP archive containing a text file added from memory, writes the ZIP to disk, re-opens it, and then uses ZipEntry.CopyToBase64 to retrieve the compressed data for the text file as a Base64-encoded string.

The Base64 string returned by CopyToBase64 represents the compressed bytes stored for the entry inside the ZIP archive. It is not the original uncompressed text data.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- ------------------------------------------------------------
-- First create a sample ZIP archive containing a small text file.

local zip = chilkat.Zip.new()

local success = zip:new_zip("c:/temp/sampleText.zip")
if not success then
    print(zip.last_error_text)
    return
end

-- Add a small text file from memory.
-- The text is stored in the ZIP as "hello.txt".
success = zip:add_string("hello.txt", "Hello from a ZIP entry!", "utf-8")
if not success then
    print(zip.last_error_text)
    return
end

-- Write the ZIP archive to disk and close it.
success = zip:write_zip_and_close()
if not success then
    print(zip.last_error_text)
    return
end

-- ------------------------------------------------------------
-- Now open the ZIP archive we just created.

local zip2 = chilkat.Zip.new()

success = zip2:open_zip("c:/temp/sampleText.zip")
if not success then
    print(zip2.last_error_text)
    return
end

-- Get the text file entry.
local entry = chilkat.ZipEntry.new()

success = zip2:entry_of("hello.txt", entry)

if not success then
    print("Entry not found.")
    return
end

-- Get the compressed ZIP entry data as Base64.
--
-- This is the compressed data stored inside the ZIP file,
-- not the uncompressed text "Hello from a ZIP entry!".
local b64 = entry:copy_to_base64()

print("Base64 compressed ZIP entry data:")
print(b64)

zip2:close_zip()

print("Done.")