Adding upstream version 3.5.1.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
e37d4622a7
commit
097626e61a
57 changed files with 6023 additions and 0 deletions
67
filecompressor.go
Normal file
67
filecompressor.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
package archiver
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// FileCompressor can compress and decompress single files.
|
||||
type FileCompressor struct {
|
||||
Compressor
|
||||
Decompressor
|
||||
|
||||
// Whether to overwrite existing files when creating files.
|
||||
OverwriteExisting bool
|
||||
}
|
||||
|
||||
// CompressFile reads the source file and compresses it to destination.
|
||||
// The destination must have a matching extension.
|
||||
func (fc FileCompressor) CompressFile(source, destination string) error {
|
||||
if err := fc.CheckExt(destination); err != nil {
|
||||
return err
|
||||
}
|
||||
if fc.Compressor == nil {
|
||||
return fmt.Errorf("no compressor specified")
|
||||
}
|
||||
if !fc.OverwriteExisting && fileExists(destination) {
|
||||
return fmt.Errorf("file exists: %s", destination)
|
||||
}
|
||||
|
||||
in, err := os.Open(source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer in.Close()
|
||||
|
||||
out, err := os.Create(destination)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
return fc.Compress(in, out)
|
||||
}
|
||||
|
||||
// DecompressFile reads the source file and decompresses it to destination.
|
||||
func (fc FileCompressor) DecompressFile(source, destination string) error {
|
||||
if fc.Decompressor == nil {
|
||||
return fmt.Errorf("no decompressor specified")
|
||||
}
|
||||
if !fc.OverwriteExisting && fileExists(destination) {
|
||||
return fmt.Errorf("file exists: %s", destination)
|
||||
}
|
||||
|
||||
in, err := os.Open(source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer in.Close()
|
||||
|
||||
out, err := os.Create(destination)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
return fc.Decompress(in, out)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue