项目作者: golift

项目描述 :
Go Library for Queuing and Extracting Archives: Rar, Zip, 7zip, Gz, Tar, Tgz, Bz2, Tbz2
高级语言: Go
项目地址: git://github.com/golift/xtractr.git
创建时间: 2020-02-16T12:07:50Z
项目社区:https://github.com/golift/xtractr

开源协议:MIT License

下载


xtractr

Go Library for Queuing and Extracting ZIP, RAR, GZ, BZ2, TAR,
TGZ, TBZ2, 7Z, ISO (and other) compressed archive files.
Can also be used ad-hoc for direct decompression and extraction. See docs.

  • GoDoc
  • Works on Linux, Windows, FreeBSD and macOS without Cgo.
  • Supports 32 and 64 bit architectures.
  • Decrypts RAR and 7-Zip archives with passwords.

Interface

This library provides a queue, and a common interface to extract files.
It does not do the heavy lifting, and relies on these libraries to extract files:

Zip, Gzip, Tar and Bzip are all handled by the standard Go library.

Examples

Example 1 - Queue

  1. package main
  2. import (
  3. "log"
  4. "os"
  5. "strings"
  6. "golift.io/xtractr"
  7. )
  8. // Logger satisfies the xtractr.Logger interface.
  9. type Logger struct {
  10. xtractr *log.Logger
  11. debug *log.Logger
  12. info *log.Logger
  13. }
  14. // Printf satisfies the xtractr.Logger interface.
  15. func (l *Logger) Printf(msg string, v ...interface{}) {
  16. l.xtractr.Printf(msg, v...)
  17. }
  18. // Debug satisfies the xtractr.Logger interface.
  19. func (l *Logger) Debugf(msg string, v ...interface{}) {
  20. l.debug.Printf(msg, v...)
  21. }
  22. // Infof printf an info line.
  23. func (l *Logger) Infof(msg string, v ...interface{}) {
  24. l.info.Printf(msg, v...)
  25. }
  26. func main() {
  27. log := &Logger{
  28. xtractr: log.New(os.Stdout, "[XTRACTR] ", 0),
  29. debug: log.New(os.Stdout, "[DEBUG] ", 0),
  30. info: log.New(os.Stdout, "[INFO] ", 0),
  31. }
  32. q := xtractr.NewQueue(&xtractr.Config{
  33. Suffix: "_xtractd",
  34. Logger: log,
  35. Parallel: 1,
  36. FileMode: 0644, // ignored for tar files.
  37. DirMode: 0755,
  38. })
  39. defer q.Stop() // Stop() waits until all extractions finish.
  40. response := make(chan *xtractr.Response)
  41. // This sends an item into the extraction queue (buffered channel).
  42. q.Extract(&xtractr.Xtract{
  43. Name: "my archive", // name is not import to this library.
  44. SearchPath: "/tmp/archives", // can also be a direct file.
  45. CBChannel: response, // queue responses are sent here.
  46. })
  47. // Queue always sends two responses. 1 on start and again when finished (error or not)
  48. resp := <-response
  49. log.Infof("Extraction started: %s", strings.Join(resp.Archives, ", "))
  50. resp = <-response
  51. if resp.Error != nil {
  52. // There is possibly more data in the response that is useful even on error.
  53. // ie you may want to cleanup any partial extraction.
  54. log.Printf("Error: %v", resp.Error)
  55. }
  56. log.Infof("Extracted Files:\n - %s", strings.Join(resp.NewFiles, "\n - "))
  57. }

Example 2 - Direct

This example shows ExtractFile() with a very simple XFile.
You can choose output path, as well as file and dir modes.
Failing to provide OutputDir results in unexpected behavior.
ExtractFile() attempts to identify the type of file. If you
know the file type you may call the direct method instead:

  • ExtractZIP(*XFile)
  • ExtractRAR(*XFile)
  • ExtractTar(*XFile)
  • ExtractGzip(*XFile)
  • ExtractBzip(*XFile)
  • ExtractTarGzip(*XFile)
  • ExtractTarBzip(*XFile)
  • Extract7z(*XFile)
  1. package main
  2. import (
  3. "log"
  4. "strings"
  5. "golift.io/xtractr"
  6. )
  7. func main() {
  8. x := &xtractr.XFile{
  9. FilePath: "/tmp/myfile.zip",
  10. OutputDir: "/tmp/myfile", // do not forget this.
  11. }
  12. // size is how many bytes were written.
  13. // files may be nil, but will contain any files written (even with an error).
  14. size, files, err := xtractr.ExtractFile(x)
  15. if err != nil || files == nil {
  16. log.Fatal(size, files, err)
  17. }
  18. log.Println("Bytes written:", size, "Files Extracted:\n -", strings.Join(files, "\n -"))
  19. }

This is what XFile looks like (today at least):

  1. // XFile defines the data needed to extract an archive.
  2. type XFile struct {
  3. FilePath string // Path to archive being extracted.
  4. OutputDir string // Folder to extract archive into.
  5. FileMode os.FileMode // Write files with this mode.
  6. DirMode os.FileMode // Write folders with this mode.
  7. Password string // (RAR/7z) Archive password. Blank for none.
  8. }