main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Command line utility for the lz4 package.
  2. package main
  3. import (
  4. // "bytes"
  5. "flag"
  6. "fmt"
  7. "io"
  8. "log"
  9. "os"
  10. "path"
  11. "runtime"
  12. "strings"
  13. "github.com/pierrec/lz4"
  14. )
  15. func main() {
  16. // Process command line arguments
  17. var (
  18. blockMaxSizeDefault = 4 << 20
  19. flagStdout = flag.Bool("c", false, "output to stdout")
  20. flagDecompress = flag.Bool("d", false, "decompress flag")
  21. flagBlockMaxSize = flag.Int("B", blockMaxSizeDefault, "block max size [64Kb,256Kb,1Mb,4Mb]")
  22. flagBlockDependency = flag.Bool("BD", false, "enable block dependency")
  23. flagBlockChecksum = flag.Bool("BX", false, "enable block checksum")
  24. flagStreamChecksum = flag.Bool("Sx", false, "disable stream checksum")
  25. flagHighCompression = flag.Bool("9", false, "enabled high compression")
  26. )
  27. flag.Usage = func() {
  28. fmt.Fprintf(os.Stderr, "Usage:\n\t%s [arg] [input]...\n\tNo input means [de]compress stdin to stdout\n\n", os.Args[0])
  29. flag.PrintDefaults()
  30. }
  31. flag.Parse()
  32. // Use all CPUs
  33. runtime.GOMAXPROCS(runtime.NumCPU())
  34. zr := lz4.NewReader(nil)
  35. zw := lz4.NewWriter(nil)
  36. zh := lz4.Header{
  37. BlockDependency: *flagBlockDependency,
  38. BlockChecksum: *flagBlockChecksum,
  39. BlockMaxSize: *flagBlockMaxSize,
  40. NoChecksum: *flagStreamChecksum,
  41. HighCompression: *flagHighCompression,
  42. }
  43. worker := func(in io.Reader, out io.Writer) {
  44. if *flagDecompress {
  45. zr.Reset(in)
  46. if _, err := io.Copy(out, zr); err != nil {
  47. log.Fatalf("Error while decompressing input: %v", err)
  48. }
  49. } else {
  50. zw.Reset(out)
  51. zw.Header = zh
  52. if _, err := io.Copy(zw, in); err != nil {
  53. log.Fatalf("Error while compressing input: %v", err)
  54. }
  55. if err := zw.Close(); err != nil {
  56. log.Fatalf("Error while closing stream: %v", err)
  57. }
  58. }
  59. }
  60. // No input means [de]compress stdin to stdout
  61. if len(flag.Args()) == 0 {
  62. worker(os.Stdin, os.Stdout)
  63. os.Exit(0)
  64. }
  65. // Compress or decompress all input files
  66. for _, inputFileName := range flag.Args() {
  67. outputFileName := path.Clean(inputFileName)
  68. if !*flagStdout {
  69. if *flagDecompress {
  70. outputFileName = strings.TrimSuffix(outputFileName, lz4.Extension)
  71. if outputFileName == inputFileName {
  72. log.Fatalf("Invalid output file name: same as input: %s", inputFileName)
  73. }
  74. } else {
  75. outputFileName += lz4.Extension
  76. }
  77. }
  78. inputFile, err := os.Open(inputFileName)
  79. if err != nil {
  80. log.Fatalf("Error while opening input: %v", err)
  81. }
  82. outputFile := os.Stdout
  83. if !*flagStdout {
  84. outputFile, err = os.Create(outputFileName)
  85. if err != nil {
  86. log.Fatalf("Error while opening output: %v", err)
  87. }
  88. }
  89. worker(inputFile, outputFile)
  90. inputFile.Close()
  91. if !*flagStdout {
  92. outputFile.Close()
  93. }
  94. }
  95. }