8000 GitHub - ag-go/go-cabinet: Pure Go Microsoft Cabinet File Reader & Writer · GitHub
[go: up one dir, main page]

Skip to content

About

Pure Go Microsoft Cabinet File Reader & Writer

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

go-cabinet: Pure Go Microsoft Cabinet File Reader & Writer

Go Reference Codecov

A pure Go library for reading, writing, and extracting Microsoft Cabinet (.cab) files - commonly used in Windows Update packages, driver distributions, and software installers.

See the Microsoft Cabinet File Format specification for details.

Installation

go get github.com/abemedia/go-cabinet

Usage

Reading / Extracting a Cabinet File

r, err := cabinet.OpenReader("archive.cab")
if err != nil {
  return err
}
defer r.Close()

for _, f := range r.Files {
    rc, err := f.Open()
    if err != nil {
      return err
    }

    out, err := os.Create(f.Name)
    if err != nil {
      return err
    }

    if _, err := io.Copy(out, rc); err != nil {
      return err
    }

    if err := out.Close(); err != nil {
      return err
    }

    if err := rc.Close(); err != nil {
      return err
    }
}

The Reader also implements fs.FS, so it works with fs.WalkDir, fs.ReadFile, and other standard library functions:

fs.WalkDir(r, ".", func(path string, d fs.DirEntry, err error) error {
    fmt.Println(path)
    return err
})
78B9

Writing a Cabinet File

f, err := os.Create("archive.cab")
if err != nil {
  return err
}
defer f.Close()

w := cabinet.NewWriter(f)

wr, err := w.Create("hello.txt")
if err != nil {
  return err
}

if _, err = wr.Write([]byte("hello world")); err != nil {
  return err
}

// Make sure to check the error on Close.
if err := w.Close(); err != nil {
  return err
}

If you want to pack any fs.FS in one go, use AddFS:

if err := w.AddFS(os.DirFS("./my-app-files")); err != nil {
  return err
}

You can also add any local file or directory using AddPath:

if err := w.AddPath("hello.txt", "./hello.txt"); err != nil {
  return err
}
if err := w.AddPath("", "./my-app-files"); err != nil {
  return err
}

MSZIP is used unless another method is selected with SetCompression. LZX produces noticeably smaller cabinets than MSZIP but compresses more slowly:

if err := w.SetCompression(cabinet.LZX); err != nil {
  return err
}

See the package documentation for further examples.

Supported Compression Algorithms

  • None (uncompressed)
  • MSZIP (default)
  • LZX (2 MiB window by default)

The Quantum compression method is not implemented, but a codec for it can be registered with RegisterCompressor and RegisterDecompressor.

Limitations

  • Single-cabinet only - multi-cabinet spanning (split archives across multiple .cab files) is not supported.
  • Reserved fields - the reader skips reserved header/folder/data fields and the writer does not produce them (post-processing tools such as SignTool add these).

About

Pure Go Microsoft Cabinet File Reader & Writer

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0