@Test public void testExtractMetadata() { final String TEST_FILE = "item.pdf"; File dir = new File(testDir, "extractmetadata"); dir.mkdir(); try{ FileUtils.copyFileToDirectory( new File(packagerResources, TEST_FILE), dir); } catch(IOException ex){ fail(ex.getMessage()); } try { File metaDir = new File(testDir, "tmp"); if(Packager.createBag(dir) != null && Packager.extractMetadata(dir, metaDir)){ assertTrue(new File(metaDir, "tagmanifest-md5.txt").exists()); assertTrue(new File(metaDir, "bag-info.txt").exists()); assertTrue(new File(metaDir, "manifest-md5.txt").exists()); assertTrue(new File(metaDir, "bagit.txt").exists()); } else{ fail("Problem extracting to metadata"); } } catch(Exception ex) { ex.printStackTrace(); fail(ex.getMessage()); } }
public static boolean extractMetadata(File bagDir, File metaDir) { boolean result = false; Path bagPath = bagDir.toPath(); try (DirectoryStream<Path> stream = Files.newDirectoryStream(bagPath)) { for (Path entry : stream) { String entryFileName = entry.getFileName().toString(); if (Files.isDirectory(entry)) { if (entryFileName.equals("data")) { Path metaDirPath = Paths.get(metaDir.toURI()); File emptyDataDir = metaDirPath.resolve("data").toFile(); emptyDataDir.mkdir(); } else { FileUtils.copyDirectoryToDirectory(entry.toFile(), metaDir); } } else if (!Files.isDirectory(entry)) { FileUtils.copyFileToDirectory(entry.toFile(), metaDir); } } result = true; } catch (IOException e) { System.out.println(e.toString()); result = false; } return result; }
Packager { public static boolean extractMetadata(File bagDir, File metaDir) { boolean result = false; Path bagPath = bagDir.toPath(); try (DirectoryStream<Path> stream = Files.newDirectoryStream(bagPath)) { for (Path entry : stream) { String entryFileName = entry.getFileName().toString(); if (Files.isDirectory(entry)) { if (entryFileName.equals("data")) { Path metaDirPath = Paths.get(metaDir.toURI()); File emptyDataDir = metaDirPath.resolve("data").toFile(); emptyDataDir.mkdir(); } else { FileUtils.copyDirectoryToDirectory(entry.toFile(), metaDir); } } else if (!Files.isDirectory(entry)) { FileUtils.copyFileToDirectory(entry.toFile(), metaDir); } } result = true; } catch (IOException e) { System.out.println(e.toString()); result = false; } return result; } }
Packager { public static boolean extractMetadata(File bagDir, File metaDir) { boolean result = false; Path bagPath = bagDir.toPath(); try (DirectoryStream<Path> stream = Files.newDirectoryStream(bagPath)) { for (Path entry : stream) { String entryFileName = entry.getFileName().toString(); if (Files.isDirectory(entry)) { if (entryFileName.equals("data")) { Path metaDirPath = Paths.get(metaDir.toURI()); File emptyDataDir = metaDirPath.resolve("data").toFile(); emptyDataDir.mkdir(); } else { FileUtils.copyDirectoryToDirectory(entry.toFile(), metaDir); } } else if (!Files.isDirectory(entry)) { FileUtils.copyFileToDirectory(entry.toFile(), metaDir); } } result = true; } catch (IOException e) { System.out.println(e.toString()); result = false; } return result; }  }
Packager { public static boolean extractMetadata(File bagDir, File metaDir) { boolean result = false; Path bagPath = bagDir.toPath(); try (DirectoryStream<Path> stream = Files.newDirectoryStream(bagPath)) { for (Path entry : stream) { String entryFileName = entry.getFileName().toString(); if (Files.isDirectory(entry)) { if (entryFileName.equals("data")) { Path metaDirPath = Paths.get(metaDir.toURI()); File emptyDataDir = metaDirPath.resolve("data").toFile(); emptyDataDir.mkdir(); } else { FileUtils.copyDirectoryToDirectory(entry.toFile(), metaDir); } } else if (!Files.isDirectory(entry)) { FileUtils.copyFileToDirectory(entry.toFile(), metaDir); } } result = true; } catch (IOException e) { System.out.println(e.toString()); result = false; } return result; }  static Bag createBag(File dir); static boolean validateBag(File dir); static boolean addMetadata(File bagDir,
                                      String depositMetadata,
                                      String vaultMetadata,
                                      String fileTypeMetadata,
                                      String externalMetadata); static boolean addMetaFile(File tagManifest, Path metadataDirPath, String metadataFileName, String metadata, SupportedAlgorithm alg); static String computeFileHash(File file, SupportedAlgorithm alg); static boolean extractMetadata(File bagDir, File metaDir); static void main(String[] args); }
Packager { public static boolean extractMetadata(File bagDir, File metaDir) { boolean result = false; Path bagPath = bagDir.toPath(); try (DirectoryStream<Path> stream = Files.newDirectoryStream(bagPath)) { for (Path entry : stream) { String entryFileName = entry.getFileName().toString(); if (Files.isDirectory(entry)) { if (entryFileName.equals("data")) { Path metaDirPath = Paths.get(metaDir.toURI()); File emptyDataDir = metaDirPath.resolve("data").toFile(); emptyDataDir.mkdir(); } else { FileUtils.copyDirectoryToDirectory(entry.toFile(), metaDir); } } else if (!Files.isDirectory(entry)) { FileUtils.copyFileToDirectory(entry.toFile(), metaDir); } } result = true; } catch (IOException e) { System.out.println(e.toString()); result = false; } return result; }  static Bag createBag(File dir); static boolean validateBag(File dir); static boolean addMetadata(File bagDir,
                                      String depositMetadata,
                                      String vaultMetadata,
                                      String fileTypeMetadata,
                                      String externalMetadata); static boolean addMetaFile(File tagManifest, Path metadataDirPath, String metadataFileName, String metadata, SupportedAlgorithm alg); static String computeFileHash(File file, SupportedAlgorithm alg); static boolean extractMetadata(File bagDir, File metaDir); static void main(String[] args); static final String metadataDirName; static final String depositMetaFileName; static final String vaultMetaFileName; static final String fileTypeMetaFileName; static final String externalMetaFileName; }