-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
201 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// SPDX-FileCopyrightText: 2024 Shun Sakai | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/sorairolake/lzip-go" | ||
) | ||
|
||
const version = "0.3.1" | ||
|
||
type options struct { | ||
version bool | ||
stdout bool | ||
decompress bool | ||
keep bool | ||
dictionarySize uint | ||
} | ||
|
||
var opt options | ||
|
||
func init() { | ||
flag.BoolVar(&opt.version, "version", false, "Print version number") | ||
flag.BoolVar(&opt.stdout, "stdout", false, "Write to standard output, keep input files") | ||
flag.BoolVar(&opt.decompress, "decompress", false, "Decompress data") | ||
flag.BoolVar(&opt.keep, "keep", false, "Keep input files") | ||
flag.UintVar(&opt.dictionarySize, "dictionary-size", lzip.DefaultDictSize, "Set dictionary size in bytes") | ||
|
||
flag.Usage = func() { | ||
fmt.Fprintf(flag.CommandLine.Output(), "Usage: glzip [OPTIONS] <FILE>...\n") | ||
flag.PrintDefaults() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-FileCopyrightText: 2024 Shun Sakai | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
package main | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"os" | ||
|
||
"github.com/sorairolake/lzip-go" | ||
) | ||
|
||
func compress(file string, output *os.File, opt options) error { | ||
if !opt.stdout { | ||
out, err := os.Create(file + ".lz") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
output = out | ||
} | ||
|
||
defer output.Close() | ||
|
||
writerOpt := &lzip.WriterOptions{DictSize: uint32(opt.dictionarySize)} | ||
|
||
bufWriter := bufio.NewWriter(output) | ||
defer bufWriter.Flush() | ||
|
||
writer, err := lzip.NewWriterOptions(bufWriter, writerOpt) | ||
if err != nil { | ||
return err | ||
} | ||
defer writer.Close() | ||
|
||
input, err := os.Open(file) | ||
if err != nil { | ||
return err | ||
} | ||
defer input.Close() | ||
|
||
if _, err := io.Copy(writer, input); err != nil { | ||
return err | ||
} | ||
|
||
if !opt.keep { | ||
if err := os.Remove(file); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-FileCopyrightText: 2024 Shun Sakai | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
// Glzip reduces the size of files. It reads and writes of lzip format | ||
// compressed files. | ||
// | ||
// Usage: | ||
// | ||
// glzip [OPTIONS] <FILE>... | ||
// | ||
// Arguments: | ||
// | ||
// <FILE> | ||
// Input file. | ||
// | ||
// Options: | ||
// | ||
// -stdout | ||
// Write to standard output, keep input files. | ||
// -decompress | ||
// Decompress data. | ||
// -keep | ||
// Keep input files. | ||
// -dictionary-size <BYTE> | ||
// Set dictionary size in bytes. | ||
// -version | ||
// Print version number. | ||
package main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// SPDX-FileCopyrightText: 2024 Shun Sakai | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
package main | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/sorairolake/lzip-go" | ||
) | ||
|
||
func uncompress(file string, output *os.File, opt options) error { | ||
input, err := os.Open(file) | ||
if err != nil { | ||
return err | ||
} | ||
defer input.Close() | ||
|
||
bufReader := bufio.NewReader(input) | ||
|
||
reader, err := lzip.NewReader(bufReader) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !opt.stdout { | ||
out, err := os.Create(strings.TrimSuffix(file, ".lz")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
output = out | ||
} | ||
|
||
defer output.Close() | ||
|
||
if _, err := io.Copy(output, reader); err != nil { | ||
return err | ||
} | ||
|
||
if !opt.keep { | ||
if err := os.Remove(file); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.