private static void writeZipInputToFile(final ZipInputStream zipInputstream, final File outputFile) throws FileNotFoundException, IOException { // The input is a file. An FileOutputStream is created to write the content of the new file. mkdirs(outputFile.getParentFile()); try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile)) { // The contents of the new file, that is read from the ZipInputStream using a buffer (byte []), is written. int bytesRead; final byte[] buffer = new byte[BUFFER_SIZE]; while ((bytesRead = zipInputstream.read(buffer)) > -1) { fileOutputStream.write(buffer, 0, bytesRead); } fileOutputStream.flush(); } catch (final IOException ioe) { // In case of error, the file is deleted outputFile.delete(); throw ioe; } }
private static void writeZipInputToFile(final ZipInputStream zipInputstream, final File outputFile) throws FileNotFoundException, IOException { // The input is a file. An FileOutputStream is created to write the content of the new file. mkdirs(outputFile.getParentFile()); try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile)) { // The contents of the new file, that is read from the ZipInputStream using a buffer (byte []), is written. int bytesRead; final byte[] buffer = new byte[BUFFER_SIZE]; while ((bytesRead = zipInputstream.read(buffer)) > -1) { fileOutputStream.write(buffer, 0, bytesRead); } fileOutputStream.flush(); } catch (final IOException ioe) { // In case of error, the file is deleted outputFile.delete(); throw ioe; } }
private static void extractZipEntries(final ZipInputStream zipInputstream, final File outputFolder) throws IOException { ZipEntry zipEntry; while ((zipEntry = zipInputstream.getNextEntry()) != null) { try { // For each entry, a file is created in the output directory "folder" final File outputFile = new File(outputFolder.getAbsolutePath(), zipEntry.getName()); // If the entry is a directory, it creates in the output folder, and we go to the next entry (continue). if (zipEntry.isDirectory()) { mkdirs(outputFile); continue; } writeZipInputToFile(zipInputstream, outputFile); } finally { zipInputstream.closeEntry(); } } }
private static void extractZipEntries(final ZipInputStream zipInputstream, final File outputFolder) throws IOException { ZipEntry zipEntry; while ((zipEntry = zipInputstream.getNextEntry()) != null) { try { // For each entry, a file is created in the output directory "folder" final File outputFile = new File(outputFolder.getAbsolutePath(), zipEntry.getName()); // If the entry is a directory, it creates in the output folder, and we go to the next entry (continue). if (zipEntry.isDirectory()) { mkdirs(outputFile); continue; } writeZipInputToFile(zipInputstream, outputFile); } finally { zipInputstream.closeEntry(); } } }
public static File createTempDirectory(final URI directoryPath) { final File tmpDir = new File(directoryPath); tmpDir.setReadable(true); tmpDir.setWritable(true); mkdirs(tmpDir); try { // to initialize internal class FilenameUtils. Otherwise it cannot load the class as the shutdown is in progress: FileUtils.isSymlink(tmpDir); } catch (IOException ignored) { } try { Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { final boolean deleted = deleteDir(tmpDir); if (!deleted) { System.err.println( "Unable to delete directory: " + tmpDir + ". Trying with an alternative force delete."); FileUtils.forceDelete(tmpDir); } } catch (final IOException e) { throw new BonitaRuntimeException(e); } })); } catch (IllegalStateException ignored) { // happen in case of hook already registered and when shutting down } return tmpDir; }
public static File createTempDirectory(final URI directoryPath) { final File tmpDir = new File(directoryPath); tmpDir.setReadable(true); tmpDir.setWritable(true); mkdirs(tmpDir); try { // to initialize internal class FilenameUtils. Otherwise it cannot load the class as the shutdown is in progress: FileUtils.isSymlink(tmpDir); } catch (IOException ignored) { } try { Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { final boolean deleted = deleteDir(tmpDir); if (!deleted) { System.err.println( "Unable to delete directory: " + tmpDir + ". Trying with an alternative force delete."); FileUtils.forceDelete(tmpDir); } } catch (final IOException e) { throw new BonitaRuntimeException(e); } })); } catch (IllegalStateException ignored) { // happen in case of hook already registered and when shutting down } return tmpDir; }