ZipWriter

Package: Libraries.Data.Compression · Inherits from: Libraries.Language.Object

The ZipWriter class writes a ZIP archive entry by entry. Entries are written in the order they are added, text is stored as UTF-8, PNG images can be drawn pixel by pixel and added as entries, and an entry can hold the MD5 checksum of some text. ZipWriter creates archives, the work is done by java.util.zip and javax.imageio through the plugin plugins.quorum.Libraries.Data.Compression.ZipWriter.

Example Code

use Libraries.Data.Compression.ZipWriter

class Main
action Main
    ZipWriter zip
    zip:Open("Outputs/example.zip")
    zip:AddText("readme.txt", "Hello")
    zip:BeginImage(2, 2)
    zip:SetPixel(0, 0, 255, 0, 0, 255)
    zip:AddImage("red-dot.png")
    zip:AddCopy("copy-of-red-dot.png", "red-dot.png")
    zip:AddMd5("readme.txt.md5", "hello")
    zip:Close()
end
end

Actions

AddCopy(text entryName, text existingEntryName)

Adds another entry with exactly the same bytes as an entry added earlier.

Parameters

  • entryName text: The name of the new entry.
  • existingEntryName text: The entry to copy.

Example

use Libraries.Data.Compression.ZipWriter

ZipWriter zip
zip:Open("Outputs/plate.zip")
zip:BeginImage(2, 2)
zip:AddImage("Metadata/plate_1.png")
zip:AddCopy("Metadata/plate_no_light_1.png", "Metadata/plate_1.png")
zip:Close()

AddImage(text entryName)

Encodes the current image as PNG and adds it as an entry.

Parameters

  • entryName text: The name inside the archive.

Example

use Libraries.Data.Compression.ZipWriter

ZipWriter zip
zip:Open("Outputs/dot.zip")
zip:BeginImage(1, 1)
zip:SetPixel(0, 0, 255, 0, 0, 255)
zip:AddImage("Metadata/dot.png")
zip:Close()

AddMd5(text entryName, text content)

Adds a text entry holding the MD5 checksum of the content's UTF-8 bytes: 32 upper-case hexadecimal digits, the form Bambu Studio writes in Metadata/plate_1.gcode.md5. The content itself is not added.

Parameters

  • entryName text: The name of the checksum entry.
  • content text: The text to checksum.

Example

use Libraries.Data.Compression.ZipWriter

text gcode = "G28"
ZipWriter zip
zip:Open("Outputs/print.zip")
zip:AddMd5("Metadata/plate_1.gcode.md5", gcode)
zip:AddText("Metadata/plate_1.gcode", gcode)
zip:Close()

AddText(text entryName, text content)

Adds one entry whose content is the text, encoded as UTF-8.

Parameters

  • entryName text: The name inside the archive, such as "Metadata/plate_1.gcode".
  • content text: The text to store.

Example

use Libraries.Data.Compression.ZipWriter

ZipWriter zip
zip:Open("Outputs/print.zip")
zip:AddText("Metadata/plate_1.gcode", "G28 ;home all axes")
zip:Close()

BeginImage(integer width, integer height)

Starts a blank, fully transparent RGBA image. Sizes below 1 become 1.

Parameters

  • width integer: The width in pixels.
  • height integer: The height in pixels.

Example

use Libraries.Data.Compression.ZipWriter

ZipWriter zip
zip:Open("Outputs/thumbnail.zip")
zip:BeginImage(128, 128)
zip:SetPixel(64, 64, 255, 255, 255, 255)
zip:AddImage("thumbnail.png")
zip:Close()

Close()

Finishes the archive. Nothing in it can be read until this is called. Calling it again does nothing.

Example

use Libraries.Data.Compression.ZipWriter

ZipWriter zip
zip:Open("Outputs/readme.zip")
zip:AddText("readme.txt", "Finished archives can be opened by any ZIP tool.")
zip:Close()

Open(text path)

Creates (or overwrites) the archive, creating its folder if needed. An archive that was still open is finished first.

Parameters

  • path text: Where to write the archive.

Example

use Libraries.Data.Compression.ZipWriter

ZipWriter zip
zip:Open("Outputs/notes.zip")
zip:AddText("notes.txt", "Hello")
zip:Close()

SetPixel(integer x, integer y, integer red, integer green, integer blue, integer alpha)

Sets one pixel of the current image. Pixels outside the image are ignored, and each channel is clamped to 0 to 255.

Parameters

  • x integer: The column, from the left.
  • y integer: The row, from the top.
  • red integer: The red channel.
  • green integer: The green channel.
  • blue integer: The blue channel.
  • alpha integer: The opacity, 255 being opaque.

Example

use Libraries.Data.Compression.ZipWriter

ZipWriter zip
zip:Open("Outputs/square.zip")
zip:BeginImage(8, 8)
integer x = 2
repeat while x < 6
    zip:SetPixel(x, 2, 0, 128, 255, 255)
    x = x + 1
end
zip:AddImage("square.png")
zip:Close()

Inherited Actions

3 actions from Libraries.Language.Object