LibraryOfCongress / bagit-java

Java library to support the BagIt specification.
Other
73 stars 49 forks source link

addFileAsPayload removed? #130

Closed nicolasfranck closed 5 years ago

nicolasfranck commented 5 years ago

When submitting an issue please include:

Problem

From version 5 on, it is not possible anymore to add payload to an existing bag? Or can this be done elsewhere?

jscancella commented 5 years ago

There is no direct function to add to an existing bag. However it is easy enough to do:

Bag bag = new Bag();
Path fileToAdd = Paths.get("YOUR FILE TO ADD");

for(Manifest manifest : bag.getPayLoadManifests()) {
    MessageDigest messageDigest = MessageDigest.getInstance(manifest.getAlgorithm().getMessageDigestName());
    manifest.getFileToChecksumMap().put(fileToAdd, Hasher.hash(fileToAdd, messageDigest));
}

don't forget to write it to disk when you are done!

BagWriter.write(bag, bag.getRootDir());
nicolasfranck commented 5 years ago

So you also have to write the payload file to the data directory? With the possibility that you're doing something wrong?

jscancella commented 5 years ago

Perhaps it would help if you told me what you are trying to do?

The typical use case for the library of congress (when I was there) is to receive bags and ensure they are complete and correct. Adding files to an already existing bag object doesn't fit with this and so this java library was intentionally designed to not include builder pattern objects.

If you really need the ability to add and remove files (i.e. the Builder pattern) I would suggest either opening a new ticket, or better yet try coding it up yourself and submitting a pull request.

nicolasfranck commented 5 years ago

Strange, so the application "bagger" (https://github.com/LibraryOfCongress/bagger) is limited to version 4.12?

jscancella commented 5 years ago

I was the only one who worked on "bagger" while I was at the LoC. Not sure if they have put anyone on it since I have left (I assume they haven't). So I would consider "bagger" a legacy application that isn't supported, but would be happy to be proven wrong.

"bagger" wraps the bagit-java library and so while it uses an old version it isn't limited to that version since it handles the logic for adding files/folders and only uses bagit-java to read/write/verify bags.