exploder

package module
v0.0.0-...-ba48eb0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2023 License: BSD-3-Clause Imports: 26 Imported by: 0

README

go-exploder

This is a generalized archive exploder which can take an array of archive formats and expand them for inspection.

Archive formats supported:

  • 7zip
  • bzip2
  • cab
  • cpio
  • debian
  • gzip bgzf apk
  • iso9660
  • lzma
  • rar
  • rpm
  • tar
  • xz
  • zip aar docx epub ipa jar kmz maff msix odp ods odt pk3 pk4 pptx usdz vsdx xlsx xpi
  • zstd

Example

Here is an example of an infinite explosion. To get a finite number of layers extracted, set -1 to a value like 2 or 3.

  fh, err := os.Open("testdata.zip") // Open a file
  if err != nil {
    log.Fatal(err)
  }
  stat, err := fh.Stat() // Stat the file to get the size
  if err != nil {
    log.Fatal(err)
  }

  outputPath := "output/"

  err = exploder.Explode(outputPath, fh, stat.Size(), -1)

After this has processed, a folder named output will be created with layers upon layers of files in them.

Documentation

Documentation and usage can be found at:

https://pkg.go.dev/github.com/pschou/go-exploder

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var Debug bool

Functions

func Explode

func Explode(basePath string, in io.Reader, size int64, recursion int) (err error)

Explode the archive by looking at the file MagicBytes and then try that archive reader so as to extract layers of archives all at once.

Some layers are represented in a single extraction, while others, like tgz are actually two layers, a gzip on the first and a tar on the second. If a file is unable to be extracted it will be saved as the original name and bytes in the corresponding child folder.

One MUST provide an io.Reader and SHOULD provide the Size of provided reader for extraction. If the size is not known, use -1.

The Explode can work on io.Reader alone, such as an incoming stream from a web upload. In such cases the size can be set to -1 if it is unknown.

The filePath is the directory in which the extracted content should be placed.

Important: If one is reading from a slow media source (like a disk), a bufio.Buffer will help performance. Something like this:

fh, err := os.Open("myArchive")
stat, _ := fh.Stat()
err = exploder.Explode(data, bufio.NewReader(file), stat.Size(), 10)
Example
fh, err := os.Open("testdata.zip") // Open a file
if err != nil {
	log.Fatal(err)
}
stat, err := fh.Stat() // Stat the file to get the size
if err != nil {
	log.Fatal(err)
}

outputPath := "output/"

err = exploder.Explode(outputPath, fh, stat.Size(), -1)
Output:

func ExplodeFunc

func ExplodeFunc(HandleFile func(filePath string, r *FileReader) error, in io.Reader, size int64, recursion int) (err error)

Similar to the Explode, but provides a custom call back function for each file.

Types

type FileReader

type FileReader struct {
	// contains filtered or unexported fields
}

FileReader provides the ability to read the underlying bytes from a file in an archive.

func (*FileReader) Read

func (r *FileReader) Read(p []byte) (n int, err error)

Read implement the io.Reader interface

func (*FileReader) Size

func (r *FileReader) Size() int64

Return the size if known, other wise determine the size by reading off the buffer and then returning the size.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL