go-cabinet
Read, write and extract Microsoft Cabinet (.cab) files, with MSZIP and LZX compression and io/fs support
About go-cabinet
go-cabinet: Pure Go Microsoft Cabinet File Reader & Writer
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
})
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
.cabfiles) 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
SignTooladd these).
Frequently Asked Questions
What is go-cabinet?
go-cabinet is a File Handling library for the Go programming language. Read, write and extract Microsoft Cabinet (.cab) files, with MSZIP and LZX compression and io/fs support
How do I install go-cabinet?
Install go-cabinet with the Go module system using `go get abemedia/go-cabinet`. Check the repository for the current installation instructions.
What category does go-cabinet belong to?
go-cabinet is listed under File Handling, specifically File Handling.