goxadmaster

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 2, 2023 License: LGPL-2.1 Imports: 5 Imported by: 0

README

goxadmaster

Golang binding for a C wrapper of a forked project of XADMaster.

Installation

Requirements

C wrapper installation.

Usage

Using goxadmaster is very similar to using the C wrapper.

Creating an Archive

Has to be deallocated with Destroy method after use.

archive := goxadmaster.NewArchive("./fixtures/easy.zip")
if archive.Err != nil {
    return archive.Err
}
Archive Set methods

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)
List

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
}
Entry set method

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)
}
Batch

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)
Extraction

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
}
Errors

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())
    }
}
Destroy

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
}

Example

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
}

Documentation

Index

Constants

View Source
const POINTER_SIZE = bits.UintSize / 8

Variables

This section is empty.

Functions

func DestroyList

func DestroyList(entries []IEntry) error

Destroys a slice of Entries.

Parameters:

  • entries - slice of Entries to be destroyed.

Returns:

  • error if any of the Entry has already been destroyed.

Types

type Archive

type Archive struct {
	Err error
	// contains filtered or unexported fields
}

func NewArchive

func NewArchive(path string) *Archive

Creates a new Archive. Has to be deallocated with Destroy method after use.

Parameters:

  • path - path to the existing Archive.

Returns:

  • pointer to a new instance of Archive.

func (*Archive) Destroy

func (ego *Archive) Destroy() error

Destroys the Archive.

Returns:

  • error, if Archive has been already destroyed, nil otherwise.

func (*Archive) Extract

func (ego *Archive) Extract(entries []IEntry)

Extract from the Archive. If batch > -1, only batch Entries are extracted.

Parameters:

  • entries - slice of Entries.

func (*Archive) List

func (ego *Archive) List() []IEntry

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.

Returns:

  • slice of Entries.

func (*Archive) SetAlwaysOverwritesFiles

func (ego *Archive) SetAlwaysOverwritesFiles(alwaysOverwriteFiles bool)

Sets if always overwrite files if they are present on the destination path.

Parameters:

  • alwaysOverwriteFiles

func (*Archive) SetAlwaysSkipsFiles

func (ego *Archive) SetAlwaysSkipsFiles(alwaysSkipsFiles bool)

Sets if always skip files on error.

Parameters:

  • alwaysSkipsFiles

func (*Archive) SetBatch

func (ego *Archive) SetBatch(batch int, entries []IEntry)

Sets the batch, which specifies how many Entries to extract. If batch <= -1, everything will be extracted at once.

Parameters:

  • batch - number of Entries,
  • entries - the slice of Entries.

func (*Archive) SetCopiesArchiveModificationTimeToEnclosingDirectory

func (ego *Archive) SetCopiesArchiveModificationTimeToEnclosingDirectory(copiesArchiveModificationTimeToEnclosingDirectory bool)

Sets if to set Entries modification time also to the destination files.

Parameters:

  • copiesArchiveModificationTimeToEnclosingDirectory

func (*Archive) SetDestination

func (ego *Archive) SetDestination(path string)

Sets default destination for Entries at extraction. Destination setting is important for not renamed Entries only. Otherwise ignored.

Parameters:

  • path - the destination path.

func (*Archive) SetEncodingName

func (ego *Archive) SetEncodingName(encodingName string)

Sets default encoding name for Entries at extraction.

Parameters:

  • encodingName - the encoding name.

func (*Archive) SetExtractsSubArchives

func (ego *Archive) SetExtractsSubArchives(extractsSubArchives bool)

Sets if extract also included subarchives. Not recommended set to yes - unsufficient testing.

Parameters:

  • extractsSubArchives

func (*Archive) SetMacResourceForkStyle

func (ego *Archive) SetMacResourceForkStyle(macResourceForkStyle bool)

Sets if to use MacOS forking style. Not recommended - not tested on Linux, just for completeness.

Parameters:

  • macResourceForkStyle

func (*Archive) SetPassword

func (ego *Archive) SetPassword(password string)

Sets default password for Entries at extraction.

Parameters:

  • password - the password.

func (*Archive) SetPasswordEncodingName

func (ego *Archive) SetPasswordEncodingName(passEncodingName string)

Sets default password encoding name for Entries at extraction.

Parameters:

  • passEncodingName - the password encoding name.

func (*Archive) SetPropagatesRelevantMetadata

func (ego *Archive) SetPropagatesRelevantMetadata(propagatesRelevantMetadata bool)

Sets if propagate relevant metadata (passwords etc.). Not recommended set to yes - unsufficient testing.

Parameters:

  • propagatesRelevantMetadata

type Entry

type Entry struct {
	// contains filtered or unexported fields
}

func (*Entry) Destroy

func (ego *Entry) Destroy() error

Destroys individual Entry.

Returns:

  • error if Entry has been already destroyed.

func (*Entry) GetCorrupted

func (ego *Entry) GetCorrupted() bool

Predicate - is corrupted?

func (*Entry) GetDir

func (ego *Entry) GetDir() bool

Predicate - is directory?

func (*Entry) GetEid

func (ego *Entry) GetEid() uint32

Get Entry unique identifier.

func (*Entry) GetEncoding

func (ego *Entry) GetEncoding() string

Get Entry detected encoding.

func (*Entry) GetEncrypted

func (ego *Entry) GetEncrypted() bool

Predicate - is encrypted by using of password?

func (*Entry) GetError

func (ego *Entry) GetError() error

Get error record.

func (*Entry) GetFilename

func (ego *Entry) GetFilename() string

Get full path with filename within the archive.

func (ego *Entry) GetLink() bool

Predicate - is link?

func (*Entry) GetRenaming

func (ego *Entry) GetRenaming() string

Get Entry renaming.

You may set Entry destination by hand by setting this.

func (*Entry) GetResource

func (ego *Entry) GetResource() bool

Predicate - is a resource?

func (*Entry) GetSize

func (ego *Entry) GetSize() uint

Get unpacked size.

func (*Entry) SetRenaming

func (ego *Entry) SetRenaming(renaming string)

Sets renaming for the Entry from constant string and allocates copy.

Parameters:

  • renaming - path with a new name.

type IArchive

type IArchive interface {
	List() []IEntry
	Extract([]IEntry)
	SetBatch(int, []IEntry)
	Destroy() error
	SetDestination(string)
	SetPassword(string)
	SetEncodingName(string)
	SetPasswordEncodingName(string)
	SetAlwaysOverwritesFiles(bool)
	SetAlwaysSkipsFiles(bool)
	SetExtractsSubArchives(bool)
	SetPropagatesRelevantMetadata(bool)
	SetCopiesArchiveModificationTimeToEnclosingDirectory(bool)
	SetMacResourceForkStyle(bool)
}

type IEntry

type IEntry interface {
	GetFilename() string
	GetDir() bool
	GetLink() bool
	GetResource() bool
	GetCorrupted() bool
	GetEncrypted() bool
	GetEid() uint32
	GetEncoding() string
	GetRenaming() string
	GetError() error
	GetSize() uint
	SetRenaming(string)
	Destroy() error
	// contains filtered or unexported methods
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL