Compare commits
4 commits
Author | SHA1 | Date | |
---|---|---|---|
72721eddcd | |||
7fc67a10c9 | |||
4eb6158348 | |||
6f66040e09 |
5 changed files with 87 additions and 2 deletions
|
@ -8,7 +8,7 @@ The whole goal with this library is to create something that can both read and w
|
|||
## Roadmap
|
||||
- [x] Automatically detect the archive format
|
||||
- [x] Add mutexes to work with concurrency
|
||||
- [ ] Support RAR format
|
||||
- [ ] Support RAR format (will be read-only)
|
||||
- [ ] Support zstd format
|
||||
- [ ] Add writing capability
|
||||
- [ ] Support multithreaded gzip
|
||||
|
|
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)
|
||||
|
||||
|
|
2
go.mod
2
go.mod
|
@ -1,4 +1,4 @@
|
|||
module egtyl.xyz/omnibill/archiver
|
||||
module egtyl.xyz/shane/archiver
|
||||
|
||||
go 1.23.2
|
||||
|
||||
|
|
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