public void dirToZip(final Path srcDir, Path destZip) throws IOException, URISyntaxException {

    logger.debug("packaging " + srcDir + " -> " + destZip);

    if (Files.exists(destZip)) {
      logger.info("deleting existing " + destZip);
      Files.delete(destZip);
    }

    // destZip.toUri() does not work
    URI zipLocation = new URI("jar:file:" + destZip);

    // create zip
    Map<String, String> env = new HashMap<String, String>();
    env.put("create", "true");
    final FileSystem zipFs = FileSystems.newFileSystem(zipLocation, env);

    // copy recursively
    Files.walkFileTree(
        srcDir,
        new SimpleFileVisitor<Path>() {
          public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
              throws IOException {
            return copy(file);
          }

          public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
              throws IOException {
            return copy(dir);
          }

          private FileVisitResult copy(Path src) throws IOException {
            Path dest = src.subpath(srcDir.getNameCount() - 1, src.getNameCount());
            Path destInZip = zipFs.getPath(dest.toString());

            // permissions are not necessarily correct though, so they are also reset in comp
            Files.copy(
                src,
                destInZip,
                new CopyOption[] {
                  StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING
                });
            return FileVisitResult.CONTINUE;
          }
        });

    zipFs.close();
  }
예제 #2
0
  private void insertFile(
      Path apkPath, Map<String, String> zip_properties, File insert, String method, Path location)
      throws AndrolibException, IOException {
    // ZipFileSystem only writes at .close()
    // http://mail.openjdk.java.net/pipermail/nio-dev/2012-July/001764.html
    try (FileSystem fs = FileSystems.newFileSystem(apkPath, null)) {
      Path root = fs.getPath("/");

      // in order to get the path relative to the zip, we strip off the absolute path, minus what we
      // already have in the zip. thus /var/files/apktool/apk/unknown/folder/file => /folder/file
      Path dest =
          fs.getPath(root.toString(), insert.getAbsolutePath().replace(location.toString(), ""));
      Path newFile = Paths.get(insert.getAbsolutePath());
      Files.copy(newFile, dest, StandardCopyOption.REPLACE_EXISTING);
      fs.close();
    }
  }
예제 #3
0
  private void insertFolder(
      Path apkPath, Map<String, String> zip_properties, File insert, String method, Path location)
      throws AndrolibException, IOException {
    try (FileSystem fs = FileSystems.newFileSystem(apkPath, null)) {
      Path root = fs.getPath("/");
      Path dest =
          fs.getPath(root.toString(), insert.getAbsolutePath().replace(location.toString(), ""));
      Path parent = dest.normalize();

      // check for folder existing in apkFileSystem
      if (parent != null && Files.notExists(parent)) {
        if (!Files.isDirectory(parent, LinkOption.NOFOLLOW_LINKS)) {
          Files.createDirectories(parent);
        }
      }
      fs.close();
    }
  }
예제 #4
0
  @Override
  public void download(String remote, String local, Collection<Option> options) throws IOException {
    local = ValidateUtils.checkNotNullAndNotEmpty(local, "Invalid argument local: %s", local);

    ClientSession session = getClientSession();
    FactoryManager manager = session.getFactoryManager();
    FileSystemFactory factory = manager.getFileSystemFactory();
    FileSystem fs = factory.createFileSystem(session);
    try {
      download(remote, fs, fs.getPath(local), options);
    } finally {
      try {
        fs.close();
      } catch (UnsupportedOperationException e) {
        // Ignore
      }
    }
  }
예제 #5
0
  /** Example of usage of other than default filesystem */
  @Test
  public void testZipFileSystem() throws IOException {
    Path zipFile = getFileKeeper().copyResource(Paths.get("archived.zip"));
    assertTrue(Files.exists(zipFile));

    // chooses ZipFileSystemProvider
    FileSystem zipFS = FileSystems.newFileSystem(zipFile, null);
    assertNotNull("ZIP file system not installed", zipFS);

    Path root = zipFS.getPath("/");
    assertNotNull(root);
    assertTrue(Files.exists(root));

    List<Path> zipDirPaths = iterableToList(Files.newDirectoryStream(root));
    assertNotNull(zipDirPaths);
    // two files and one directory
    assertEquals(3, zipDirPaths.size());
    assertTrue(zipDirPaths.contains(zipFS.getPath("/mama.txt")));
    assertTrue(zipDirPaths.contains(zipFS.getPath("/dokument.docx")));
    assertTrue(zipDirPaths.contains(zipFS.getPath("/podpriecinok/")));
    // because Paths uses default filesystem
    assertFalse(zipDirPaths.contains(Paths.get("/mama.txt")));
    // file system need to be closed
    zipFS.close();

    // create new file archive
    // this dont work for me
    //		Map<String, String> attrs = new HashMap<>();
    //		attrs.put("create", "true"); // create of not exists, default is false
    //		// another possible attribute is "encoding"
    //
    //
    //		URI u = URI.create("zip:/test.zip");
    //		Path p =Paths.get(u);
    //		FileSystem tmpFS = FileSystems.newFileSystem(u, attrs);
    //
    //		tmpFS.close();
    //		getFileKeeper().markForWatch(emptyZip);
  }
예제 #6
0
 @After
 public void after() throws Exception {
   fileSystem.close();
 }
예제 #7
0
 @Override
 public void close() throws IOException {
   fileSystem.close();
 }