You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.4 KiB
68 lines
1.4 KiB
5 years ago
|
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)
|
||
|
}
|