Documentation ¶
Overview ¶
Package xtractr provides methods and procedures to extract compressed archive files. It can be used in two ways. The simplest method is to pass an archive file path and an output path to ExtractFile(). This decompresses the provided file and returns some information about the data written.
The other, more sophisticated way to extract files is with a queue. The queue method allows you to send an Xtract into a channel where it's queued up and extracted in order. The number of concurrent extractions is configured when the queue is created. A provided callback method is run when a queued Xtract begins and it's run again when the Xtract finishes.
Index ¶
- Constants
- Variables
- func Difference(slice1 []string, slice2 []string) []string
- func Extract7z(xFile *XFile) (int64, []string, []string, error)
- func ExtractBzip(xFile *XFile) (int64, []string, error)
- func ExtractFile(xFile *XFile) (int64, []string, []string, error)
- func ExtractGzip(xFile *XFile) (int64, []string, error)
- func ExtractISO(xFile *XFile) (int64, []string, error)
- func ExtractRAR(xFile *XFile) (int64, []string, []string, error)
- func ExtractTar(xFile *XFile) (int64, []string, error)
- func ExtractTarBzip(xFile *XFile) (int64, []string, error)
- func ExtractTarGzip(xFile *XFile) (int64, []string, error)
- func ExtractZIP(xFile *XFile) (int64, []string, error)
- func ExtractZIPNew(xFile *XFile) (int64, []string, error)
- func FindCompressedFiles(filter Filter) map[string][]string
- type Config
- type Exclude
- type Filter
- type Logger
- type Response
- type XFile
- type Xtract
- type Xtractr
- func (x *Xtractr) DeleteFiles(files ...string)
- func (x *Xtractr) Extract(extract *Xtract) (int, error)
- func (x *Xtractr) GetFileList(path string) ([]string, error)
- func (x *Xtractr) MoveFiles(fromPath string, toPath string, overwrite bool) ([]string, error)
- func (x *Xtractr) Rename(oldpath, newpath string) error
- func (x *Xtractr) Start() error
- func (x *Xtractr) Stop()
Constants ¶
const ( DefaultDirMode = 0o755 DefaultFileMode = 0o644 DefaultSuffix = "_xtractr" // DefaultBufferSize is the size of the extraction buffer. // ie. How many jobs can be queued before things get slow. DefaultBufferSize = 1000 )
Sane defaults.
Variables ¶
var ( ErrQueueStopped = fmt.Errorf("extractor queue stopped, cannot extract") ErrNoCompressedFiles = fmt.Errorf("no compressed files found") ErrUnknownArchiveType = fmt.Errorf("unknown archive file type") ErrInvalidPath = fmt.Errorf("archived file contains invalid path") ErrInvalidHead = fmt.Errorf("archived file contains invalid header file") ErrQueueRunning = fmt.Errorf("extractor queue running, cannot start") ErrNoConfig = fmt.Errorf("call NewQueue() to initialize a queue") ErrNoLogger = fmt.Errorf("xtractr.Config.Logger must be non-nil") )
Custom errors returned by this module.
Functions ¶
func Difference ¶
Difference returns all the strings that are in slice2 but not in slice1. Used to find new files in a file list from a path. ie. those we extracted. This is a helper method and only exposed for convenience. You do not have to call this.
func Extract7z ¶
Extract7z extracts a 7zip archive. Volumes: https://github.com/bodgit/sevenzip/issues/54
func ExtractBzip ¶
ExtractBzip extracts a bzip2-compressed file. That is, a single file.
func ExtractFile ¶
ExtractFile calls the correct procedure for the type of file being extracted. Returns size of extracted data, list of extracted files, list of archives processed, and/or error.
func ExtractGzip ¶
ExtractGzip extracts a gzip-compressed file. That is, a single file.
func ExtractISO ¶
ExtractISO writes an ISO's contents to disk.
func ExtractTar ¶
ExtractTar extracts a raw (non-compressed) tar archive.
func ExtractTarBzip ¶
ExtractTarBzip extracts a bzip2-compressed tar archive.
func ExtractTarGzip ¶
ExtractTarGzip extracts a gzip-compressed tar archive.
func ExtractZIP ¶
ExtractZIP extracts a zip file.. to a destination. Simple enough.
func ExtractZIPNew ¶
ExtractZIP extracts a zip file.. to a destination. Simple enough.
func FindCompressedFiles ¶
FindCompressedFiles returns all the rar and zip files in a path. This attempts to grab only the first file in a multi-part archive. Sometimes there are multiple archives, so if the archive does not have "part" followed by a number in the name, then it will be considered an independent archive. Some packagers seem to use different naming schemes, so this will need to be updated as time progresses. So far it's working well. This is a helper method and only exposed for convenience. You do not have to call this.
Types ¶
type Config ¶
type Config struct { // Size of the extraction channel buffer. Default=1000. // Use -1 for unbuffered channel. Not recommend. BuffSize int // Number of concurrent extractions allowed. Parallel int // Filemode used when writing files, tar ignores this, so does Windows. FileMode os.FileMode // Filemode used when writing folders, tar ignores this. DirMode os.FileMode // The suffix used for temporary folders. Suffix string // Logs are sent to this Logger. Logger }
Config is the input data to configure the Xtract queue. Fill this out and pass it into NewQueue() to create a queue for archive extractions.
type Filter ¶
type Filter struct { // This is the path to search in for archives. Path string // Any files with this suffix are ignored. ie. ".7z" or ."iso" ExcludeSuffix Exclude }
Filter is the input to find compressed files.
type Response ¶
type Response struct { // Extract Started (false) or Finished (true). Done bool // Size of data written. Size int64 // Temporary output folder. Output string // Items still in queue. Queued int // When this extract began. Started time.Time // Elapsed extraction duration. ie. How long it took. Elapsed time.Duration // Extra archives extracted from within an archive. Extras map[string][]string // Initial archives found and extracted. Archives map[string][]string // Files written to final path. NewFiles []string // Error encountered, only when done=true. Error error // Copied from input data. X *Xtract }
Response is sent to the call-back function. The first CBFunction call is just a notification that the extraction has started. You can determine it's the first call by chcking Response.Done. false = started, true = finished. When done=false the only other meaningful data provided is the re.Archives, re.Output and re.Queue.
type XFile ¶
type XFile struct { // Path to archive being extracted. FilePath string // Folder to extract archive into. OutputDir string // Write files with this mode. FileMode os.FileMode // Write folders with this mode. DirMode os.FileMode // (RAR/7z/Zip) Archive password. Blank for none. Gets prepended to Passwords, below. Password string // (RAR/7z) Archive passwords (to try multiple). Passwords []string }
XFile defines the data needed to extract an archive.
type Xtract ¶
type Xtract struct { // Unused in this app; exposed for calling library. Name string // Archive password. Only supported with RAR and 7zip files. Prepended to Passwords. Password string // Archive passwords (try multiple). Only supported with RAR and 7zip files. Passwords []string // Folder path and filters describing where and how to find archives. Filter // Set RecurseISO to true if you want to recursively extract archives in ISO files. // If ISOs and other archives are found, none will not extract recursively if this is false. RecurseISO bool // Folder to extract data. Default is same level as SearchPath with a suffix. ExtractTo string // Leave files in temporary folder? false=move files back to Searchpath // Moving files back will cause the "extracted files" returned to only contain top-level items. TempFolder bool // Delete Archives after successful extraction? Be careful. DeleteOrig bool // Create a log (.txt) file of the extraction information. LogFile bool // Callback Function, runs twice per queued item. CBFunction func(*Response) // Callback Channel, msg sent twice per queued item. CBChannel chan *Response }
Xtract defines the queue input data: data needed to extract files in a path. Fill this out to create a queued extraction and pass it into Xtractr.Extract(). If a CBFunction is provided it runs when the queued extract begins w/ Response.Done=false. The CBFunction is called again when the extraction finishes w/ Response.Done=true. The CBFunction channel works the exact same way, except it's a channel instead of a blocking function.
type Xtractr ¶
type Xtractr struct {
// contains filtered or unexported fields
}
Xtractr is what you get from NewQueue(). This is the main app struct. Use this struct to call Xtractr.Extract() to queue an extraction.
func NewQueue ¶
NewQueue returns a new Xtractr Queue you can send Xtract jobs into. This is where to start if you're creating an extractor queue. You must provide a Logger in the config, everything else is optional.
func (*Xtractr) DeleteFiles ¶
DeleteFiles obliterates things and logs. Use with caution.
func (*Xtractr) Extract ¶
Extract is how external code begins an extraction process against a path. To add an item to the extraction queue, create an Xtract struct with the search path set and pass it to this method. The current queue size is returned.
func (*Xtractr) GetFileList ¶
GetFileList returns all the files in a path. This is non-resursive and only returns files _in_ the base path provided. This is a helper method and only exposed for convenience. You do not have to call this.
func (*Xtractr) MoveFiles ¶
MoveFiles relocates files then removes the folder they were in. Returns the new file paths. This is a helper method and only exposed for convenience. You do not have to call this.
func (*Xtractr) Rename ¶
Rename is an attempt to deal with "invalid cross link device" on weird file systems.