A Java library for working with and creating BagIt packages
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.gitignore
.travis.yml
LICENSE.txt
README.md
pom.xml

README.md

freelib-bagit  Build Status Codacy Badge Known Vulnerabilities Maven Javadocs

Freelib-bagit is a Java library for working with and creating BagIt packages. It supports most of the BagIt spec, but does not yet support fetch.txt files.

For more on the BagIt spec, start with: http://en.wikipedia.org/wiki/BagIt

How to use freelib-bagIt:

From within a Java program, you can create a new bag:

Bag bag = new Bag("dryad_630");

or create a bag from an existing BagIt directory:

Bag bag = new Bag("/home/kevin/dryad_630");

or open a pre-existing gz, bzip2, or zip compressed bag:

Bag bag = new Bag("dryad_630.tar.gz");

You can add metadata to the bag:

Bag bag = new Bag("dryad_630");
BagInfo bagInfo = bag.getBagInfo();

bagInfo.addMetadata(BagInfoTags.CONTACT_NAME, "Kevin S. Clarke");
bagInfo.addMetadata(BagInfoTags.CONTACT_PHONE, "ksclarke@gmail.com");
bagInfo.addMetadata("Alt-Email", "thetrashcan@gmail.com");

or add metadata via Java Properties:

Properties metadata = new Properties();
File properties = new File("my.properties");

try {
    metadata.load(new FileReader(properties));
}
catch (IOException details) {
    System.out.println(details.getMessage());
}

Bag bag = new Bag("bag_new", metadata);

You can add data to the bag:

Bag bag = new Bag("dryad_630");
File dataFile = new File("/home/kevin/data_file.csv");
File dataDir = new File("/home/kevin/csv_files/");

bag.addData(dataFile, dataDir);

And you can validate the bag:

BagValidator validator = new BagValidator();
Bag bag = new Bag("/home/kevin/dryad_630.tar.gz");

try {
    ValidBag validBag = validator.validate(bag);
}
catch (BagException details) {
    switch (details.getReason()) {
    case BagException.PAYLOAD_MANIFEST_DIFFERS_FROM_DATADIR:
        // do something
        break;
    default:
        System.out.println(details.getMessage());
    }
}

or, simply, check a bag's validity:

BagValidator validator = new BagValidator();
Bag bag = new Bag("/home/kevin/dryad_630.tar.gz");

if (validator.isValid(bag)) {
    System.out.println("Bag is valid");
}
else {
    System.out.println("Bag isn't valid");
}

You can also write a valid bag to a compressed file for transport:

BagValidator validator = new BagValidator();
Bag bag = new Bag("/home/kevin/dryad_630");

try {
    ValidBag validBag = validator.validate(bag);
    File bz2Bag = validBag.toTarBZip2();
}
catch (BagException details) {
    System.out.println(details.getMessage());
}
catch (IOException details) {
    System.out.println(details.getMessage());
}

Contact

If you have questions about freelib-bagit feel free to ask or, if you encounter a problem, please feel free to open an issue in the project's issue queue.