mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
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.
67 lines
1.4 KiB
67 lines
1.4 KiB
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)
|
|
}
|
|
|