Add ExtractFile function
This commit is contained in:
parent
997df3585a
commit
6f66040e09
3 changed files with 85 additions and 0 deletions
11
archive.go
11
archive.go
|
@ -141,6 +141,17 @@ func (a *Archive) Extract(opts ExtractOptions) error {
|
|||
return extract(Filesystem{file: true}, opts, a)
|
||||
}
|
||||
|
||||
// ExtractFile extracts a specified file from an archive to a specified directory.
|
||||
// It takes a file path, and ExtractFileOptions as its parameters.
|
||||
// The function returns an error, if any.
|
||||
func (a *Archive) ExtractFile(path string, opts ExtractFileOptions) error {
|
||||
file, err := a.GetFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return file.Extract(opts)
|
||||
}
|
||||
|
||||
// Close closes the Archive.
|
||||
// The function returns an error, if any.
|
||||
func (a *Archive) Close() error {
|
||||
|
|
|
@ -92,30 +92,80 @@ func TestArchiveExtract(t *testing.T) {
|
|||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = os.RemoveAll(testArchiveBaseDir + "/extracted/zip")
|
||||
assert.NoError(t, err)
|
||||
|
||||
t.Log("== ExtractFileZip ==")
|
||||
err = ExtractFile(testArchiveBaseDir+"/test.zip", "test1.txt", ExtractFileOptions{
|
||||
Folder: testArchiveBaseDir + "/extracted/zip",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.FileExists(t, testArchiveBaseDir+"/extracted/zip/test1.txt")
|
||||
|
||||
t.Log("== ExtractTar ==")
|
||||
err = Extract(testArchiveBaseDir+"/test.tar", ExtractOptions{
|
||||
Folder: testArchiveBaseDir + "/extracted/tar",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = os.RemoveAll(testArchiveBaseDir + "/extracted/tar")
|
||||
assert.NoError(t, err)
|
||||
|
||||
t.Log("== ExtractFileTar ==")
|
||||
err = ExtractFile(testArchiveBaseDir+"/test.tar", "test1.txt", ExtractFileOptions{
|
||||
Folder: testArchiveBaseDir + "/extracted/tar",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.FileExists(t, testArchiveBaseDir+"/extracted/tar/test1.txt")
|
||||
|
||||
t.Log("== ExtractTarXz ==")
|
||||
err = Extract(testArchiveBaseDir+"/test.tar.xz", ExtractOptions{
|
||||
Folder: testArchiveBaseDir + "/extracted/xz",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = os.RemoveAll(testArchiveBaseDir + "/extracted/xz")
|
||||
assert.NoError(t, err)
|
||||
|
||||
t.Log("== ExtractFileTarXz ==")
|
||||
err = ExtractFile(testArchiveBaseDir+"/test.tar.xz", "test1.txt", ExtractFileOptions{
|
||||
Folder: testArchiveBaseDir + "/extracted/xz",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.FileExists(t, testArchiveBaseDir+"/extracted/xz/test1.txt")
|
||||
|
||||
t.Log("== ExtractTarBz ==")
|
||||
err = Extract(testArchiveBaseDir+"/test.tar.bz2", ExtractOptions{
|
||||
Folder: testArchiveBaseDir + "/extracted/bzip",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = os.RemoveAll(testArchiveBaseDir + "/extracted/bzip")
|
||||
assert.NoError(t, err)
|
||||
|
||||
t.Log("== ExtractFileTarBz ==")
|
||||
err = ExtractFile(testArchiveBaseDir+"/test.tar.bz2", "test1.txt", ExtractFileOptions{
|
||||
Folder: testArchiveBaseDir + "/extracted/bzip",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.FileExists(t, testArchiveBaseDir+"/extracted/bzip/test1.txt")
|
||||
|
||||
t.Log("== ExtractTarGz ==")
|
||||
err = Extract(testArchiveBaseDir+"/test.tar.gz", ExtractOptions{
|
||||
Folder: testArchiveBaseDir + "/extracted/gz",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = os.RemoveAll(testArchiveBaseDir + "/extracted/gz")
|
||||
assert.NoError(t, err)
|
||||
|
||||
t.Log("== ExtractFileTarGz ==")
|
||||
err = ExtractFile(testArchiveBaseDir+"/test.tar.gz", "test1.txt", ExtractFileOptions{
|
||||
Folder: testArchiveBaseDir + "/extracted/gz",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.FileExists(t, testArchiveBaseDir+"/extracted/gz/test1.txt")
|
||||
|
||||
err = os.RemoveAll(testArchiveBaseDir + "/extracted")
|
||||
assert.NoError(t, err)
|
||||
|
||||
|
|
24
open.go
24
open.go
|
@ -69,6 +69,30 @@ func Extract(path string, options ExtractOptions) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// ExtractFile opens an archive file and extracts a specific file within the archive.
|
||||
// It takes a path, a file path, and ExtractFileOptions for its parameters.
|
||||
// The function returns an error, if any.
|
||||
func ExtractFile(path string, file string, options ExtractFileOptions) error {
|
||||
archive, err := Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer archive.Close()
|
||||
|
||||
archiveFile, err := archive.GetFile(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := archiveFile.Extract(options); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := archive.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func openArchive(filesystem Filesystem, path string) (*Archive, error) {
|
||||
archive := new(Archive)
|
||||
archive.files = make(map[string]*File)
|
||||
|
|
Loading…
Reference in a new issue