mirror of
https://github.com/containers/podman.git
synced 2025-10-20 04:34:01 +08:00

Added some tests to verify that files extractions works with different compression format. Created a decompressor interface with 2 main methods: reader(): returns an io.Reader for the specific compression algorithm copy(): extracts the compressed file into the file provided as param Created 5 decompressor types: - gzip: extract gzip files - xz: extract xz files - zip: extract zip files - generic: extract any other file using github.com/containers/image/v5/pkg/compression - uncompressed: only do a copy of the file Minor fix to the progress bar instances: added a call to bar.Abort(false) that happens before Progress.Wait() to avoid that it hangs when a bar is not set as completed although extraction is done. Signed-off-by: Mario Loriedo <mario.loriedo@gmail.com>
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package compression
|
|
|
|
import (
|
|
"archive/zip"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type zipDecompressor struct {
|
|
compressedFilePath string
|
|
zipReader *zip.ReadCloser
|
|
fileReader io.ReadCloser
|
|
}
|
|
|
|
func newZipDecompressor(compressedFilePath string) decompressor {
|
|
return &zipDecompressor{
|
|
compressedFilePath: compressedFilePath,
|
|
}
|
|
}
|
|
|
|
func (d *zipDecompressor) srcFilePath() string {
|
|
return d.compressedFilePath
|
|
}
|
|
|
|
func (d *zipDecompressor) reader() (io.Reader, error) {
|
|
zipReader, err := zip.OpenReader(d.compressedFilePath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
d.zipReader = zipReader
|
|
if len(zipReader.File) != 1 {
|
|
return nil, errors.New("machine image files should consist of a single compressed file")
|
|
}
|
|
z, err := zipReader.File[0].Open()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
d.fileReader = z
|
|
return z, nil
|
|
}
|
|
|
|
func (*zipDecompressor) copy(w *os.File, r io.Reader) error {
|
|
_, err := io.Copy(w, r)
|
|
return err
|
|
}
|
|
|
|
func (d *zipDecompressor) close() {
|
|
if err := d.zipReader.Close(); err != nil {
|
|
logrus.Errorf("Unable to close zip file: %q", err)
|
|
}
|
|
if err := d.fileReader.Close(); err != nil {
|
|
logrus.Errorf("Unable to close zip file: %q", err)
|
|
}
|
|
}
|