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.
go get github.com/abemedia/go-cabinetr, 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
})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.
- 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.
- 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).