gpt4 book ai didi

org.sonar.api.utils.ZipUtils.unzip()方法的使用及代码示例

转载 作者:知者 更新时间:2024-03-15 12:42:40 25 4
gpt4 key购买 nike

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

ZipUtils.unzip介绍

[英]Unzip a file into a directory. The directory is created if it does not exist.
[中]将文件解压缩到目录中。如果目录不存在,则创建该目录。

代码示例

代码示例来源:origin: SonarSource/sonarqube

public static File unzip(InputStream zip, File toDir) throws IOException {
 return unzip(zip, toDir, (Predicate<ZipEntry>) ze -> true);
}

代码示例来源:origin: SonarSource/sonarqube

/**
 * Unzip a file into a directory. The directory is created if it does not exist.
 *
 * @return the target directory
 */
public static File unzip(File zip, File toDir) throws IOException {
 return unzip(zip, toDir, (Predicate<ZipEntry>) ze -> true);
}

代码示例来源:origin: SonarSource/sonarqube

/**
 * @deprecated replaced by {@link #unzip(InputStream, File, Predicate)} in 6.2.
 */
@Deprecated
public static File unzip(InputStream stream, File toDir, ZipEntryFilter filter) throws IOException {
 return unzip(stream, toDir, new ZipEntryFilterDelegate(filter));
}

代码示例来源:origin: SonarSource/sonarqube

/**
 * @deprecated replaced by {@link #unzip(File, File, Predicate)} in 6.2.
 */
@Deprecated
public static File unzip(File zip, File toDir, ZipEntryFilter filter) throws IOException {
 return unzip(zip, toDir, new ZipEntryFilterDelegate(filter));
}

代码示例来源:origin: SonarSource/sonarqube

private File unzipFile(File cachedFile) throws IOException {
  String filename = cachedFile.getName();
  File destDir = new File(cachedFile.getParentFile(), filename + "_unzip");
  File lockFile = new File(cachedFile.getParentFile(), filename + "_unzip.lock");
  if (!destDir.exists()) {
   FileOutputStream out = new FileOutputStream(lockFile);
   try {
    java.nio.channels.FileLock lock = out.getChannel().lock();
    try {
     // Recheck in case of concurrent processes
     if (!destDir.exists()) {
      File tempDir = pluginFiles.createTempDir();
      ZipUtils.unzip(cachedFile, tempDir, newLibFilter());
      FileUtils.moveDirectory(tempDir, destDir);
     }
    } finally {
     lock.release();
    }
   } finally {
    out.close();
    deleteQuietly(lockFile);
   }
  }
  return destDir;
 }
}

代码示例来源:origin: SonarSource/sonarqube

@Override
 public ExplodedPlugin explode(PluginInfo pluginInfo) {
  File tempDir = new File(fs.getTempDir(), TEMP_RELATIVE_PATH);
  File toDir = new File(tempDir, pluginInfo.getKey());
  try {
   org.sonar.core.util.FileUtils.cleanDirectory(toDir);

   File jarSource = pluginInfo.getNonNullJarFile();
   File jarTarget = new File(toDir, jarSource.getName());
   FileUtils.copyFile(jarSource, jarTarget);
   ZipUtils.unzip(jarSource, toDir, newLibFilter());
   return explodeFromUnzippedDir(pluginInfo.getKey(), jarTarget, toDir);
  } catch (Exception e) {
   throw new IllegalStateException(String.format(
    "Fail to unzip plugin [%s] %s to %s", pluginInfo.getKey(), pluginInfo.getNonNullJarFile().getAbsolutePath(), toDir.getAbsolutePath()), e);
  }
 }
}

代码示例来源:origin: SonarSource/sonarqube

/**
  * JAR files of directory extensions/plugins can be moved when server is up and plugins are uninstalled.
  * For this reason these files must not be locked by classloaders. They are copied to the directory
  * web/deploy/plugins in order to be loaded by {@link org.sonar.core.platform.PluginLoader}.
  */
 @Override
 public ExplodedPlugin explode(PluginInfo pluginInfo) {
  File toDir = new File(fs.getDeployedPluginsDir(), pluginInfo.getKey());
  try {
   forceMkdir(toDir);
   org.sonar.core.util.FileUtils.cleanDirectory(toDir);

   File jarSource = pluginInfo.getNonNullJarFile();
   File jarTarget = new File(toDir, jarSource.getName());

   FileUtils.copyFile(jarSource, jarTarget);
   ZipUtils.unzip(jarSource, toDir, newLibFilter());
   ExplodedPlugin explodedPlugin = explodeFromUnzippedDir(pluginInfo.getKey(), jarTarget, toDir);
   pluginFileSystem.addInstalledPlugin(pluginInfo, jarTarget);
   return explodedPlugin;
  } catch (Exception e) {
   throw new IllegalStateException(String.format(
    "Fail to unzip plugin [%s] %s to %s", pluginInfo.getKey(), pluginInfo.getNonNullJarFile().getAbsolutePath(), toDir.getAbsolutePath()), e);
  }
 }
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void unzipping_stream_extracts_subset_of_files() throws IOException {
 InputStream zip = urlToZip().openStream();
 File toDir = temp.newFolder();
 ZipUtils.unzip(zip, toDir, (ZipUtils.ZipEntryFilter)ze -> ze.getName().equals("foo.txt"));
 assertThat(toDir.listFiles()).containsOnly(new File(toDir, "foo.txt"));
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void fail_if_unzipping_stream_outside_target_directory() throws Exception {
 File zip = new File(getClass().getResource("ZipUtilsTest/zip-slip.zip").toURI());
 File toDir = temp.newFolder();
 expectedException.expect(IllegalStateException.class);
 expectedException.expectMessage("Unzipping an entry outside the target directory is not allowed: ../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../tmp/evil.txt");
 try (InputStream input = new FileInputStream(zip)) {
  ZipUtils.unzip(input, toDir);
 }
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void fail_if_unzipping_file_outside_target_directory() throws Exception {
 File zip = new File(getClass().getResource("ZipUtilsTest/zip-slip.zip").toURI());
 File toDir = temp.newFolder();
 expectedException.expect(IllegalStateException.class);
 expectedException.expectMessage("Unzipping an entry outside the target directory is not allowed: ../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../tmp/evil.txt");
 ZipUtils.unzip(zip, toDir);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void unzipping_file_extracts_subset_of_files() throws IOException {
 File zip = FileUtils.toFile(urlToZip());
 File toDir = temp.newFolder();
 ZipUtils.unzip(zip, toDir, (ZipUtils.ZipEntryFilter)ze -> ze.getName().equals("foo.txt"));
 assertThat(toDir.listFiles()).containsOnly(new File(toDir, "foo.txt"));
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void unzip_stream() throws Exception {
 InputStream zip = urlToZip().openStream();
 File toDir = temp.newFolder();
 ZipUtils.unzip(zip, toDir);
 assertThat(toDir.list()).hasSize(3);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void unzip_file() throws IOException {
 File zip = FileUtils.toFile(urlToZip());
 File toDir = temp.newFolder();
 ZipUtils.unzip(zip, toDir);
 assertThat(toDir.list()).hasSize(3);
}

代码示例来源:origin: SonarSource/sonarqube

@Override
 public ExplodedPlugin explode(PluginInfo info) {
  try {
   ZipUtils.unzip(jarFile, toDir, newLibFilter());
   return explodeFromUnzippedDir(info.getKey(), info.getNonNullJarFile(), toDir);
  } catch (Exception e) {
   throw new IllegalStateException(e);
  }
 }
};

代码示例来源:origin: SonarSource/sonarqube

@Test
public void unzipping_creates_target_directory_if_it_does_not_exist() throws IOException {
 File zip = FileUtils.toFile(urlToZip());
 File tempDir = temp.newFolder();
 Files.delete(tempDir);
 File subDir = new File(tempDir, "subDir");
 ZipUtils.unzip(zip, subDir);
 assertThat(subDir.list()).hasSize(3);
}

代码示例来源:origin: SonarSource/sonarqube

@Override
public void execute(ComputationStep.Context context) {
 try (DbSession dbSession = dbClient.openSession(false)) {
  Optional<CeTaskInputDao.DataStream> opt = dbClient.ceTaskInputDao().selectData(dbSession, task.getUuid());
  if (opt.isPresent()) {
   File unzippedDir = tempFolder.newDir();
   try (CeTaskInputDao.DataStream reportStream = opt.get();
      InputStream zipStream = new BufferedInputStream(reportStream.getInputStream())) {
    ZipUtils.unzip(zipStream, unzippedDir);
   } catch (IOException e) {
    throw new IllegalStateException("Fail to extract report " + task.getUuid() + " from database", e);
   }
   reportDirectoryHolder.setDirectory(unzippedDir);
   if (LOGGER.isDebugEnabled()) {
    // size is not added to context statistics because computation
    // can take time. It's enabled only if log level is DEBUG.
    try {
     String dirSize = FileUtils.byteCountToDisplaySize(FileUtils2.sizeOf(unzippedDir.toPath()));
     LOGGER.debug("Analysis report is {} uncompressed", dirSize);
    } catch (IOException e) {
     LOGGER.warn("Fail to compute size of directory " + unzippedDir, e);
    }
   }
  } else {
   throw MessageException.of("Analysis report " + task.getUuid() + " is missing in database");
  }
 }
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void zip_directory() throws IOException {
 File foo = FileUtils.toFile(getClass().getResource("/org/sonar/api/utils/ZipUtilsTest/shouldZipDirectory/foo.txt"));
 File dir = foo.getParentFile();
 File zip = temp.newFile();
 ZipUtils.zipDir(dir, zip);
 assertThat(zip).exists().isFile();
 assertThat(zip.length()).isGreaterThan(1L);
 Iterator<? extends ZipEntry> zipEntries = Iterators.forEnumeration(new ZipFile(zip).entries());
 assertThat(zipEntries).hasSize(4);
 File unzipDir = temp.newFolder();
 ZipUtils.unzip(zip, unzipDir);
 assertThat(new File(unzipDir, "bar.txt")).exists().isFile();
 assertThat(new File(unzipDir, "foo.txt")).exists().isFile();
 assertThat(new File(unzipDir, "dir1/hello.properties")).exists().isFile();
}

代码示例来源:origin: org.codehaus.sonar/sonar-plugin-api

public static File unzip(InputStream zip, File toDir) throws IOException {
 unzip(zip, toDir, new ZipEntryFilter() {
  @Override
  public boolean accept(ZipEntry entry) {
   return true;
  }
 });
 return toDir;
}

代码示例来源:origin: org.sonarsource.sonarqube/sonar-plugin-api

/**
 * Unzip a file into a directory. The directory is created if it does not exist.
 *
 * @return the target directory
 */
public static File unzip(File zip, File toDir) throws IOException {
 return unzip(zip, toDir, (Predicate<ZipEntry>) ze -> true);
}

代码示例来源:origin: org.sonarsource.sonarqube/sonar-plugin-api

/**
 * @deprecated replaced by {@link #unzip(File, File, Predicate)} in 6.2.
 */
@Deprecated
public static File unzip(File zip, File toDir, ZipEntryFilter filter) throws IOException {
 return unzip(zip, toDir, new ZipEntryFilterDelegate(filter));
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com