Golang binding for a C wrapper of a forked project of XADMaster.
C wrapper installation.
Using goxadmaster is very similar to using the C wrapper.
Has to be deallocated with Destroy method after use.
archive := goxadmaster.NewArchive("./fixtures/easy.zip")
if archive.Err != nil {
return archive.Err
}After creating the Archive, it is possible to use the following Set methods:
SetDestination(string)
SetPassword(string)
SetEncodingName(string)
SetPasswordEncodingName(string)
SetAlwaysOverwritesFiles(bool)
SetAlwaysSkipsFiles(bool)
SetExtractsSubArchives(bool)
SetPropagatesRelevantMetadata(bool)
SetCopiesArchiveModificationTimeToEnclosingDirectory(bool)
SetMacResourceForkStyle(bool)The next step is the List method. This method lists content of an archive in form of arrays. Entry records must be destroyed by DestroyList call explicitly. Alternatively, it is possible to destroy individual entries using the Destroy function.
entries := archive.List()
if archive.Err != nil {
return archive.Err
}Alternatively, Entries can be renamed using the SetRenaming method. The full path with the new name must be passed as a parameter.
If a Destination was set before the SetRenaming, it is ignored.
for i := 0; i < len(entries); i++ {
curr := entries[i]
newName := fmt.Sprintf("binary%d.bin", i)
curr.SetRenaming(pathToExtract + newName)
}After listing there is an option to use method SetBatch, which specifies how many Entries to extract.
If batch <= -1, everything will be extracted at once.
archive.SetBatch(2, entries)Here comes the time for extraction. This is done using the Extract function. If batch > -1, it is necessary to perform the extraction repeatedly.
archive.Extract(entries)
if archive.Err != nil {
return archive.Err
}Next, it is a good idea to iterate through the items to see if any of them have errors.
This can be done, for example, with the following code:
for i := 0; i < len(entries); i++ {
curr := entries[i]
err := curr.GetError()
if err != nil {
fmt.Printf("WARNING: %s, WARNING MSG: %s", curr.GetFilename(), err.Error())
}
}Firstly, Entry records must be destroyed by DestroyList call explicitly or alternatively, it is possible to destroy individual entries using the Destroy function.
err := goxadmaster.DestroyList(entries)
if err != nil {
return err
}Finally, the Archive is destroyed using the Destroy method.
err = archive.Destroy()
if err != nil {
return err
}
if archive.Err != nil {
return archive.Err
}import (
"fmt"
"github.com/SpongeData-cz/goxadmaster"
)
func example() error {
// Creates a new Archive.
archive := goxadmaster.NewArchive("./fixtures/easy.zip")
if archive.Err != nil {
return archive.Err
}
// Destination setting is important for not renamed
// entries only. Otherwise ignored.
pathToExtract := "./fixtures/extracted/"
archive.SetDestination(pathToExtract)
// Programmer may call archive Set methods here.
archive.SetAlwaysOverwritesFiles(true)
// Make slice of Entries.
entries := archive.List()
if archive.Err != nil {
return archive.Err
}
// Optional entries rename.
for i := 0; i < len(entries); i++ {
curr := entries[i]
newName := fmt.Sprintf("binary%d.bin", i)
curr.SetRenaming(pathToExtract + newName)
}
// Does extraction over the list of Entries.
// Note that you may pass just a subset of
// the original list using the SetBatch() function.
archive.Extract(entries)
if archive.Err != nil {
return archive.Err
}
// Inspection of per-Entry errors.
for i := 0; i < len(entries); i++ {
curr := entries[i]
err := curr.GetError()
if err != nil {
fmt.Printf("WARNING: %s, WARNING MSG: %s", curr.GetFilename(), err.Error())
}
}
// Correct Entries removal.
err := goxadmaster.DestroyList(entries)
if err != nil {
return err
}
// Correct Archive record deletion.
err = archive.Destroy()
if err != nil {
return err
}
if archive.Err != nil {
return archive.Err
}
return nil
}