com.thoughtworks.go.util.ZipUtil.unzip()方法的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(99)

本文整理了Java中com.thoughtworks.go.util.ZipUtil.unzip()方法的一些代码示例,展示了ZipUtil.unzip()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZipUtil.unzip()方法的具体详情如下:
包路径:com.thoughtworks.go.util.ZipUtil
类名称:ZipUtil
方法名:unzip

ZipUtil.unzip介绍

暂无

代码示例

代码示例来源:origin: gocd/gocd

public void unzip(File zip, File destDir) throws IOException {
  unzip(new ZipInputStream(new BufferedInputStream(new FileInputStream(zip))), destDir);
}

代码示例来源:origin: gocd/gocd

void usePackagedCommandRepository(ZipInputStream zipInputStream, File defaultDirectory) throws IOException {
  FileUtil.deleteDirectoryNoisily(defaultDirectory);
  zipUtil.unzip(zipInputStream, defaultDirectory);
}

代码示例来源:origin: gocd/gocd

void explodePluginJarToBundleDir(File file, File location) {
  try {
    wipePluginBundleDirectory(location);
    ZipUtil zipUtil = new ZipUtil();
    zipUtil.unzip(file, location);
  } catch (IOException e) {
    throw new RuntimeException(String.format("Failed to copy plugin jar %s to bundle location %s", file, location), e);
  }
}

代码示例来源:origin: gocd/gocd

public boolean saveFile(File dest, InputStream stream, boolean shouldUnzip, int attempt) {
  String destPath = dest.getAbsolutePath();
  try {
    LOGGER.trace("Saving file [{}]", destPath);
    if (shouldUnzip) {
      zipUtil.unzip(new ZipInputStream(stream), dest);
    } else {
      systemService.streamToFile(stream, dest);
    }
    LOGGER.trace("File [{}] saved.", destPath);
    return true;
  } catch (IOException e) {
    final String message = format("Failed to save the file to: [%s]", destPath);
    if (attempt < GoConstants.PUBLISH_MAX_RETRIES) {
      LOGGER.warn(message, e);
    } else {
      LOGGER.error(message, e);
    }
    return false;
  } catch (IllegalPathException e) {
    final String message = format("Failed to save the file to: [%s]", destPath);
    LOGGER.error(message, e);
    return false;
  }
}

代码示例来源:origin: gocd/gocd

private void cacheStaticAssets(String pluginId) {
  LOGGER.info("Caching static assets for plugin: {}", pluginId);
  String data = this.analyticsExtension.getStaticAssets(pluginId);
  if (StringUtils.isBlank(data)) {
    LOGGER.info("No static assets found for plugin: {}", pluginId);
    return;
  }
  try {
    byte[] payload = Base64.getDecoder().decode(data.getBytes());
    byte[] pluginEndpointJsContent = IOUtils.toByteArray(getClass().getResourceAsStream("/" + PLUGIN_ENDPOINT_JS));
    ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(payload));
    String assetsHash = calculateHash(payload, pluginEndpointJsContent);
    String pluginAssetsRoot = currentAssetPath(pluginId, assetsHash);
    zipUtil.unzip(zipInputStream, new File(pluginAssetsRoot));
    Files.write(Paths.get(pluginAssetsRoot, DESTINATION_JS), pluginEndpointJsContent);
    pluginAssetPaths.put(pluginId, Paths.get(pluginStaticAssetsPathRelativeToRailsPublicFolder(pluginId), assetsHash).toString());
  } catch (Exception e) {
    LOGGER.error("Failed to extract static assets from plugin: {}", pluginId, e);
    ExceptionUtils.bomb(e);
  }
}

代码示例来源:origin: gocd/gocd

public void handle(InputStream stream) throws IOException {
  ZipInputStream zipInputStream = new ZipInputStream(stream);
  LOG.info("[Agent Fetch Artifact] Downloading from '{}' to '{}'. Will read from Socket stream to compute MD5 and write to file", srcFile, destOnAgent.getAbsolutePath());
  long before = System.currentTimeMillis();
  new ZipUtil((entry, stream1) -> {
    LOG.info("[Agent Fetch Artifact] Downloading a directory from '{}' to '{}'. Handling the entry: '{}'", srcFile, destOnAgent.getAbsolutePath(), entry.getName());
    new ChecksumValidator(artifactMd5Checksums).validate(getSrcFilePath(entry), md5Hex(stream1), checksumValidationPublisher);
  }).unzip(zipInputStream, destOnAgent);
  LOG.info("[Agent Fetch Artifact] Downloading a directory from '{}' to '{}'. Took: {}ms", srcFile, destOnAgent.getAbsolutePath(), System.currentTimeMillis() - before);
}

代码示例来源:origin: gocd/gocd

@Override
  public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
    try {
      File pluginsFolder = new File(systemEnvironment.get(SystemEnvironment.AGENT_PLUGINS_PATH));

      if (pluginsFolder.exists()) {
        FileUtils.forceDelete(pluginsFolder);
      }
      zipUtil.unzip(DownloadableFile.AGENT_PLUGINS.getLocalFile(), pluginsFolder);
      defaultPluginJarLocationMonitor.initialize();
      pluginManager.startInfrastructure(false);
    } catch (IOException e) {
      LOG.warn("could not extract plugin zip", e);
    } catch (RuntimeException e) {
      LOG.warn("error while initializing agent plugins", e);
    }
  }
}

代码示例来源:origin: gocd/gocd

@Override
public void initialize() {
  cleanupOldPluginDirectories();
  try {
    File bundledPluginsDirectory = new File(systemEnvironment.get(PLUGIN_GO_PROVIDED_PATH));
    if (shouldReplaceBundledPlugins(bundledPluginsDirectory)) {
      FileUtils.cleanDirectory(bundledPluginsDirectory);
      zipUtil.unzip(getPluginsZipStream(), bundledPluginsDirectory);
    }
    pluginManager.startInfrastructure(true);
  } catch (Exception e) {
    LOG.error("Could not extract bundled plugins to default bundled directory", e);
  }
}

代码示例来源:origin: gocd/gocd

private File createPluginBundle(String bundleName) throws IOException, URISyntaxException {
    File destinationPluginBundleLocation = new File(tmpDir, bundleName);
    destinationPluginBundleLocation.mkdirs();

    URL resource = getClass().getClassLoader().getResource("defaultFiles/descriptor-aware-test-plugin.jar");
    new ZipUtil().unzip(new File(resource.toURI()), destinationPluginBundleLocation);
    return destinationPluginBundleLocation;
  }
}

代码示例来源:origin: gocd/gocd

private File explodeBundleIntoDirectory(ZipInputStream src, String destinationDir) throws IOException {
  File destinationPluginBundleLocation = new File(tmpDir, destinationDir);
  destinationPluginBundleLocation.mkdirs();
  new ZipUtil().unzip(src, destinationPluginBundleLocation);
  return destinationPluginBundleLocation;
}

代码示例来源:origin: gocd/gocd

@Test
  public void shouldAllExceptionsExceptionQuietly() throws Exception {
    doThrow(new IOException()).when(zipUtil).unzip(DownloadableFile.AGENT_PLUGINS.getLocalFile(), new File(SystemEnvironment.PLUGINS_PATH));
    try {
      doThrow(new RuntimeException("message")).when(pluginJarLocationMonitor).initialize();
      agentPluginsInitializer.onApplicationEvent(null);
    } catch (Exception e) {
      fail("should have handled IOException");
    }
  }
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldExtractPluginZip() throws Exception {
  agentPluginsInitializer.onApplicationEvent(null);
  verify(zipUtil).unzip(DownloadableFile.AGENT_PLUGINS.getLocalFile(), new File(SystemEnvironment.PLUGINS_PATH));
}

代码示例来源:origin: gocd/gocd

private File explodeBundleIntoDirectory(ZipInputStream src, String destinationDir) throws IOException, URISyntaxException {
    File destinationPluginBundleLocation = temporaryFolder.newFolder(destinationDir);
    new ZipUtil().unzip(src, destinationPluginBundleLocation);
    return destinationPluginBundleLocation;
  }
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldThrowUpWhileTryingToUnzipIfAnyOfTheFilePathsInArchiveHasAPathContainingDotDotSlashPath() throws URISyntaxException, IOException {
  try {
    zipUtil.unzip(new File(getClass().getResource("/archive_traversal_attack.zip").toURI()), destDir);
    fail("squash.zip is capable of causing archive traversal attack and hence should not be allowed.");
  } catch (IllegalPathException e) {
    assertThat(e.getMessage(), is("File ../2.txt is outside extraction target directory"));
  }
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldHandleIOExceptionQuietly() throws Exception {
  doThrow(new IOException()).when(zipUtil).unzip(DownloadableFile.AGENT_PLUGINS.getLocalFile(), new File(SystemEnvironment.PLUGINS_PATH));
  try {
    agentPluginsInitializer.onApplicationEvent(null);
  } catch (Exception e) {
    fail("should have handled IOException");
  }
}

代码示例来源:origin: gocd/gocd

@Test
@RunIf(value = OSChecker.class, arguments = OSChecker.LINUX)
public void shouldZipFileWhoseNameHasSpecialCharactersOnLinux() throws IOException {
  File specialFile = new File(srcDir, "$`#?@!()?-_{}^'~.+=[];,a.txt");
  FileUtils.writeStringToFile(specialFile, "specialFile", UTF_8);
  zipFile = zipUtil.zip(srcDir, temporaryFolder.newFile(), Deflater.NO_COMPRESSION);
  zipUtil.unzip(zipFile, destDir);
  File baseDir = new File(destDir, srcDir.getName());
  File actualSpecialFile = new File(baseDir, specialFile.getName());
  assertThat(actualSpecialFile.isFile(), is(true));
  assertThat(fileContent(actualSpecialFile), is(fileContent(specialFile)));
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldInitializePluginJarLocationMonitorAndStartPluginInfrastructureAfterPluginZipExtracted() throws Exception {
  InOrder inOrder = inOrder(zipUtil, pluginManager, pluginJarLocationMonitor);
  agentPluginsInitializer.onApplicationEvent(null);
  inOrder.verify(zipUtil).unzip(DownloadableFile.AGENT_PLUGINS.getLocalFile(), new File(SystemEnvironment.PLUGINS_PATH));
  inOrder.verify(pluginJarLocationMonitor).initialize();
  inOrder.verify(pluginManager).startInfrastructure(false);
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldZipFileAndUnzipIt() throws IOException {
  zipFile = zipUtil.zip(srcDir, temporaryFolder.newFile(), Deflater.NO_COMPRESSION);
  assertThat(zipFile.isFile(), is(true));
  zipUtil.unzip(zipFile, destDir);
  File baseDir = new File(destDir, srcDir.getName());
  assertIsDirectory(new File(baseDir, emptyDir.getName()));
  assertIsDirectory(new File(baseDir, childDir1.getName()));
  File actual1 = new File(baseDir, file1.getName());
  assertThat(actual1.isFile(), is(true));
  assertThat(fileContent(actual1), is(fileContent(file1)));
  File actual2 = new File(baseDir, childDir1.getName() + File.separator + file2.getName());
  assertThat(actual2.isFile(), is(true));
  assertThat(fileContent(actual2), is(fileContent(file2)));
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldZipFileContentsOnly() throws IOException {
  zipFile = zipUtil.zipFolderContents(srcDir, temporaryFolder.newFile(), Deflater.NO_COMPRESSION);
  assertThat(zipFile.isFile(), is(true));
  zipUtil.unzip(zipFile, destDir);
  assertIsDirectory(new File(destDir, emptyDir.getName()));
  assertIsDirectory(new File(destDir, childDir1.getName()));
  File actual1 = new File(destDir, file1.getName());
  assertThat(actual1.isFile(), is(true));
  assertThat(fileContent(actual1), is(fileContent(file1)));
  File actual2 = new File(destDir, childDir1.getName() + File.separator + file2.getName());
  assertThat(actual2.isFile(), is(true));
  assertThat(fileContent(actual2), is(fileContent(file2)));
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldZipFileContentsAndUnzipIt() throws IOException {
  zipFile = zipUtil.zip(srcDir, temporaryFolder.newFile(), Deflater.NO_COMPRESSION);
  assertThat(zipFile.isFile(), is(true));
  zipUtil.unzip(zipFile, destDir);
  File baseDir = new File(destDir, srcDir.getName());
  assertIsDirectory(new File(baseDir, emptyDir.getName()));
  assertIsDirectory(new File(baseDir, childDir1.getName()));
  File actual1 = new File(baseDir, file1.getName());
  assertThat(actual1.isFile(), is(true));
  assertThat(fileContent(actual1), is(fileContent(file1)));
  File actual2 = new File(baseDir, childDir1.getName() + File.separator + file2.getName());
  assertThat(actual2.isFile(), is(true));
  assertThat(fileContent(actual2), is(fileContent(file2)));
}

相关文章