/** * Inflate the provided {@link ZipFile} in the provided output directory. * * @param archive the {@link ZipFile} to inflate. * @param outputDirectory the directory where to inflate the archive. * @param fileName name of the file if present. * @throws IOException in case something bad happens. */ public static void inflate(ZipFile archive, Resource outputDirectory, String fileName) throws IOException { inflate(archive, outputDirectory, fileName, null, null, null, false, false); }
/** * Inflate the provided {@link ZipFile} in the provided output directory. * * @param archive the {@link ZipFile} to inflate. * @param outputDirectory the directory where to inflate the archive. * @param fileName name of the file if present. * @param external * @throws IOException in case something bad happens. */ public static void inflate( ZipFile archive, Resource outputDirectory, String fileName, boolean external) throws IOException { inflate(archive, outputDirectory, fileName, null, null, null, external, false); }
/** * Unzips a zip a file to a specified directory, deleting the zip file after unpacking. * * @param zipFile The zip file. * @param outputDirectory The directory to unpack the contents to. * * @throws IOException Any I/O errors that occur. * * TODO: move this to IOUtils */ public static void unzipFile( File zipFile, File outputDirectory ) throws IOException { if ( outputDirectory == null ) { outputDirectory = zipFile.getParentFile(); } if ( outputDirectory != null && !outputDirectory.exists() ) { outputDirectory.mkdir(); } ZipFile archive = new ZipFile(zipFile); IOUtils.inflate(archive, outputDirectory, null); IOUtils.deleteFile(zipFile); }
/** * Simple class implementing a periodic Thread that periodically tries to delete the files that * were provided to him. * * <p>It tries to delete each file at most {@link FileCleaner#maxAttempts} number of times. If * this number is exceeded it simply throws the file away notifying the users with a warning * message. * * @author Simone Giannecchini, GeoSolutions. */ public static final class FileCleaner extends Thread { /** * Maximum number of attempts to delete a given {@link File}. * * <p>If the provided number of attempts is exceeded we simply drop warn the user and we * remove the {@link File} from our list. */ private int maxAttempts = DEF_MAX_ATTEMPTS; /** Period in seconds between two checks. */ private volatile long period = DEFAULT_PERIOD; /** * Asks this {@link FileCleaner} to clean up this file. * * @param fileToDelete {@link File} that we want to permanently delete. */ public void addFile(final File fileToDelete) { // does it exists
IOUtils.inflate(archive, outputDirectory, null, workspace, store, files, external, true); zipFile.delete();
@Test public void testInflateBadEntryName() throws IOException { File destDir = temp.newFolder("d1").toPath().toFile(); destDir.mkdirs(); Resource directory = new GeoServerResourceLoader(destDir).get(""); File file = ZipTestUtil.initZipSlipFile(temp.newFile("d1.zip")); try { IOUtils.inflate(new ZipFile(file), directory, null, null, null, null, false, false); fail("Expected decompression to fail"); } catch (IOException e) { assertThat(e.getMessage(), startsWith("Entry is outside of the target directory")); } } }
IOUtils.copy( getClass().getResourceAsStream("test-data/pds.zip"), new FileOutputStream(zip)); org.geoserver.rest.util.IOUtils.inflate(new ZipFile(zip), Files.asResource(f), null);