Search in sources :

Example 1 with Md5SumComputer

use of eu.transkribus.core.io.util.Md5SumComputer in project TranskribusCore by Transkribus.

the class TrpDocPacker method packDocFiles.

/**
 * Zips a local TrpDoc into a file at the given zipFilePath.
 * The process involves computing MD5 sums for all files.
 * METS file will be included.
 *
 * @param doc
 * @param zipFilePath
 * @return
 * @throws IOException
 */
public File packDocFiles(TrpDoc doc, String zipFilePath) throws IOException {
    File localFolder = doc.getMd().getLocalFolder();
    if (localFolder == null) {
        throw new IOException("Not a local Document!");
    }
    Md5SumComputer md5Comp = new Md5SumComputer();
    md5Comp.addObserver(passthroughObserver);
    doc = md5Comp.computeAndSetMd5Sums(doc);
    if (zipFilePath == null || zipFilePath.isEmpty()) {
        logger.info("No zip file path specified.");
        zipFilePath = TEMP_DIR + File.separator + "TRP_DOC_" + System.currentTimeMillis() + ".zip";
    } else if (!(new File(zipFilePath).getParentFile().exists())) {
        throw new IllegalArgumentException(zipFilePath + " refers to a non-existent directory!");
    }
    logger.info("Creating zip file at: " + zipFilePath);
    String metsFilePath = localFolder.getAbsoluteFile() + File.separator + TrpMetsBuilder.METS_FILE_NAME;
    File metsFile = new File(metsFilePath);
    Mets mets;
    logger.info("Creating METS file at: " + metsFilePath);
    // build a mets that points to all files we need
    // 2nd arg: export page files (add to mets filesec), 3rd arg: export alto files, 4th arg: export images
    mets = TrpMetsBuilder.buildMets(doc, true, false, true, null);
    try {
        metsFile = JaxbUtils.marshalToFile(mets, metsFile, TrpDocMetadata.class);
    } catch (JAXBException e) {
        logger.error(e.getMessage(), e);
        throw new IOException("Could not create METS file.", e);
    }
    updateStatus("Built METS file");
    // prepare the list with files to be packed into the ZIP
    List<String> fileList = new LinkedList<>();
    fileList.add(TrpMetsBuilder.METS_FILE_NAME);
    // traverse the METS filesection and add all files to be zipped
    List<FileGrpType> typeGrps = MetsUtil.getMasterFileGrp(mets);
    for (FileGrpType type : typeGrps) {
        if (type.getID().equals(TrpMetsBuilder.IMG_GROUP_ID) || type.getID().equals(TrpMetsBuilder.PAGE_GROUP_ID)) {
            List<String> files = getFiles(type);
            fileList.addAll(files);
        }
    }
    updateStatus("Creating ZIP file...");
    File zipFile = ZipUtils.zip(fileList, localFolder.getAbsolutePath(), zipFilePath);
    return zipFile;
}
Also used : FileGrpType(eu.transkribus.core.model.beans.mets.FileGrpType) Md5SumComputer(eu.transkribus.core.io.util.Md5SumComputer) Mets(eu.transkribus.core.model.beans.mets.Mets) JAXBException(javax.xml.bind.JAXBException) TrpDocMetadata(eu.transkribus.core.model.beans.TrpDocMetadata) IOException(java.io.IOException) File(java.io.File) LinkedList(java.util.LinkedList)

Example 2 with Md5SumComputer

use of eu.transkribus.core.io.util.Md5SumComputer in project TranskribusCore by Transkribus.

the class TrpDocPackerTest method main.

public static void main(String[] args) {
    // final String BASE = "/mnt/dea_scratch/TRP/";
    // //	String docPath = BASE + "Bentham_box_002/";
    // String shitDoc = BASE + "Schauplatz";
    // String shitDoc = BASE + "TRPTestDoc_I_ZvS_1901_1Q";
    String shitDoc = "/mnt/iza_retro/P6080-029-019_transcriptorium/master_images/14_bozen_stadtarchiv/Ratsprotokolle Bozen 1470-1684 - Lieferung USB Platte 9-7-2013/HS 37/HS 37a";
    String zipPath = "/tmp/test.zip";
    try {
        TrpDocPacker packer = new TrpDocPacker();
        TrpDoc doc = LocalDocReader.load(shitDoc, true);
        Md5SumComputer md5Comp = new Md5SumComputer();
        // md5Comp.addObserver(passthroughObserver);
        doc = md5Comp.computeAndSetMd5Sums(doc);
        File zipFile = packer.packDocFiles(doc, zipPath);
    // 
    // TrpDoc doc2 = packer.unpackDoc(new File(unzipPath), null);
    // System.out.println(doc2);
    // TrpDoc doc = LocalDocReader.load(docPath, false);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Md5SumComputer(eu.transkribus.core.io.util.Md5SumComputer) TrpDoc(eu.transkribus.core.model.beans.TrpDoc) TrpDocPacker(eu.transkribus.core.io.TrpDocPacker) File(java.io.File)

Aggregations

Md5SumComputer (eu.transkribus.core.io.util.Md5SumComputer)2 File (java.io.File)2 TrpDocPacker (eu.transkribus.core.io.TrpDocPacker)1 TrpDoc (eu.transkribus.core.model.beans.TrpDoc)1 TrpDocMetadata (eu.transkribus.core.model.beans.TrpDocMetadata)1 FileGrpType (eu.transkribus.core.model.beans.mets.FileGrpType)1 Mets (eu.transkribus.core.model.beans.mets.Mets)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 JAXBException (javax.xml.bind.JAXBException)1