packkit/seven_z

Writing 7z with a compression method or AES-256 encryption, and reading encrypted 7z archives. packkit.write compresses with LZMA, and packkit.read reads any unencrypted 7z archive.

The writer puts every file in one LZMA, Deflate, bzip2 or Copy folder and stores files only, not directories or links. The reader also accepts LZMA2, the BCJ, BCJ2 and Delta filters, and archives with several folders.

Types

Whether and how to encrypt a 7z archive with AES-256.

The salt is empty and the IV is all zeros, so two archives made with the same password and the same contents have the same bytes. Key derivation runs 2^19 rounds of SHA-256, which takes several seconds on JavaScript.

pub type Encryption {
  Unencrypted
  EncryptContents(password: String)
  EncryptContentsAndHeaders(password: String)
}

Constructors

  • Unencrypted
  • EncryptContents(password: String)

    Encrypts the file contents. Names stay readable, like 7z a -p.

  • EncryptContentsAndHeaders(password: String)

    Encrypts the file contents and the header with the names, like 7z a -p -mhe=on.

The compression method for the folder that holds every file body.

pub type Method {
  Lzma
  Copy
  Deflate
  Bzip2
}

Constructors

  • Lzma

    LZMA with lc=3, lp=0, pb=2 and a 64 KiB dictionary, the defaults of 7z a.

  • Copy

    No compression.

  • Deflate

    Raw DEFLATE.

  • Bzip2

    bzip2.

Values

pub fn decode_with_password(
  bytes bytes: BitArray,
  password password: String,
  limits limits: limit.Limits,
) -> Result(archive.Archive, archive.ArchiveError)

Reads a 7z archive whose folders or header may be encrypted with AES-256. Folders without encryption are read as usual.

AES in 7z has no authentication tag, so a wrong password shows up as InvalidData from the decompressor that runs after it. Use packkit.read for archives without a password.

pub fn encode(
  archive_value: archive.Archive,
  method method: Method,
  encryption encryption: Encryption,
) -> Result(BitArray, archive.ArchiveError)

Writes archive as 7z with all file bodies in one folder compressed with method.

Returns EntryRejected for an empty archive and for directories and links, which this writer does not store, and CommentUnsupported when the archive has a comment.

Examples

let notes = archive.new_entry(path: "notes.txt", kind: File(<<"hi":utf8>>))
encode(
  Archive(format: SevenZ, entries: [notes], comment: None),
  method: Lzma,
  encryption: EncryptContents(password: "secret"),
)
✨ Search Document