public static void copy(File inputFile, ZipOutputStream outputFile) throws IOException { try ( FileInputStream fis = new FileInputStream(inputFile) ) { IOUtils.copy(fis, outputFile); } }
public static void copyAndClose(InputStream in, OutputStream out) throws IOException { try { IOUtils.copy(in, out); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } }
private static void copy(ZipInputStream zipStream, File to) throws IOException { try (OutputStream fos = new FileOutputStream(to)) { IOUtils.copy(zipStream, fos); } }
private static void copyStaticToOutput(String resource, File output) { InputStream is = null; OutputStream os = null; try { is = HtmlRenderer.class.getResourceAsStream("/static/" + resource); os = new FileOutputStream(new File(output, resource)); IOUtils.copy(is, os); } catch (IOException e) { throw new RuntimeException("Unable to copy static resource " + resource + " to " + output, e); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } } }
private URL expandJarAndReturnURL(JarInputStream jarStream, JarEntry entry) throws IOException { File nestedJarFile = new File(jarDir, entry.getName()); nestedJarFile.getParentFile().mkdirs(); try (FileOutputStream out = new FileOutputStream(nestedJarFile)) { IOUtils.copy(jarStream, out); } LOGGER.info("Exploded Entry {} from to {}", entry.getName(), nestedJarFile); return nestedJarFile.toURI().toURL(); }
public static byte[] tryDecompress(InputStream raw) throws Exception { try { GZIPInputStream gis = new GZIPInputStream(raw); ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(gis, out); return out.toByteArray(); } catch (Exception e) { e.printStackTrace(); } return null; }
/** * Archives specified folder into zip output stream. * * @param rootDir Root directory. * @param srcDir Source directory. * @param out Zip output stream. * @throws IOException In case of input/output exception. */ private void compressDirectoryToZip(String rootDir, String srcDir, ZipOutputStream out) throws IOException { File[] files = new File(srcDir).listFiles(); if (files != null) { for (File file : files) { if (file.isDirectory()) compressDirectoryToZip(rootDir, srcDir + File.separator + file.getName(), out); else { ZipEntry entry = new ZipEntry(srcDir.replace(rootDir, "") + File.separator + file.getName()); out.putNextEntry(entry); try (FileInputStream in = new FileInputStream(srcDir + File.separator + file.getName())) { IOUtils.copy(in, out); } } } } }
@Override public byte[] deflate(InputStream data) throws CompressionException { ByteArrayOutputStream out; GZIPOutputStream gzip = null; try { out = new ByteArrayOutputStream(); gzip = new GZIPOutputStream(out); IOUtils.copy(data, gzip); } catch (IOException e) { throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName(), e); } finally { if (gzip != null) { IOUtils.closeQuietly(gzip); } } return out.toByteArray(); }
/** * Archives specified file into zip archive. * * @param file File to be archived. * @return Byte array representing zip archive. * @throws IOException In case of input/output exception. */ private byte[] zipFile(File file) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (ZipOutputStream zos = new ZipOutputStream(baos)) { ZipEntry entry = new ZipEntry(file.getName()); zos.putNextEntry(entry); try (FileInputStream in = new FileInputStream(file.getAbsolutePath())) { IOUtils.copy(in, zos); } } return baos.toByteArray(); }
public void spill() throws IOException { if(spillBuffer == null) return; OutputStream ops = new FileOutputStream(dumpedFile); InputStream ips = new ByteArrayInputStream(spillBuffer); IOUtils.copy(ips, ops); spillBuffer = null; IOUtils.closeQuietly(ips); IOUtils.closeQuietly(ops); logger.info("Spill buffer to disk, location: {}, size = {}.", dumpedFile.getAbsolutePath(), dumpedFile.length()); }
/** * Place the data from {@link FileItem} into the file location specified by this {@link FilePath} object. */ public void copyFrom(FileItem file) throws IOException, InterruptedException { if(channel==null) { try { file.write(writing(new File(remote))); } catch (IOException e) { throw e; } catch (Exception e) { throw new IOException(e); } } else { try (InputStream i = file.getInputStream(); OutputStream o = write()) { org.apache.commons.io.IOUtils.copy(i,o); } } }
@Override public void flatMap(FileCopyTask task, Collector<Object> out) throws Exception { LOGGER.info("Processing task: " + task); Path outPath = new Path(targetPath, task.getRelativePath()); FileSystem targetFs = targetPath.getFileSystem(); // creating parent folders in case of a local FS if (!targetFs.isDistributedFS()) { //dealing with cases like file:///tmp or just /tmp File outFile = outPath.toUri().isAbsolute() ? new File(outPath.toUri()) : new File(outPath.toString()); File parentFile = outFile.getParentFile(); if (!parentFile.mkdirs() && !parentFile.exists()) { throw new RuntimeException("Cannot create local file system directories: " + parentFile); } } FSDataOutputStream outputStream = null; FSDataInputStream inputStream = null; try { outputStream = targetFs.create(outPath, FileSystem.WriteMode.OVERWRITE); inputStream = task.getPath().getFileSystem().open(task.getPath()); int bytes = IOUtils.copy(inputStream, outputStream); bytesCounter.add(bytes); } finally { IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); } fileCounter.add(1L); } });
@edu.umd.cs.findbugs.annotations.SuppressWarnings( value = "OBL_UNSATISFIED_OBLIGATION", justification = "Lombok construct of @Cleanup is handing this, but not detected by FindBugs") private static void addFilesToZip(File zipFile, List<File> filesToAdd) throws IOException { try { @Cleanup OutputStream archiveStream = new FileOutputStream(zipFile); @Cleanup ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, archiveStream); for (File fileToAdd : filesToAdd) { ZipArchiveEntry entry = new ZipArchiveEntry(fileToAdd.getName()); archive.putArchiveEntry(entry); @Cleanup BufferedInputStream input = new BufferedInputStream(new FileInputStream(fileToAdd)); IOUtils.copy(input, archive); archive.closeArchiveEntry(); } archive.finish(); } catch (ArchiveException e) { throw new IOException("Issue with creating archive", e); } }
private int exec(String cmd) throws InterruptedException, IOException { ProcessBuilder pb = new ProcessBuilder(exe, cmd); pb.redirectErrorStream(true); Process p = pb.start(); p.getOutputStream().close(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); copy(p.getInputStream(), baos); int r = p.waitFor(); if (r!=0) LOGGER.info(exe+" cmd: output:\n"+baos); return r; }
public static void cpdir(File src, File dest) throws BrutException { dest.mkdirs(); File[] files = src.listFiles(); for (int i = 0; i < files.length; i++) { File file = files[i]; File destFile = new File(dest.getPath() + File.separatorChar + file.getName()); if (file.isDirectory()) { cpdir(file, destFile); continue; } try { InputStream in = new FileInputStream(file); OutputStream out = new FileOutputStream(destFile); IOUtils.copy(in, out); in.close(); out.close(); } catch (IOException ex) { throw new BrutException("Could not copy file: " + file, ex); } } }
private void copyResource(String name) throws IOException { String simpleName = name.substring(name.lastIndexOf('/') + 1); try (InputStream input = classLoader.getResourceAsStream(name); OutputStream output = new FileOutputStream(new File(outputDirectory, simpleName))) { IOUtils.copy(input, output); } }
public static byte[] unGzipBytes(final byte[] bytes) throws IOException { final ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes); final GZIPInputStream gzipInputStream = new GZIPInputStream(byteInputStream); final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); IOUtils.copy(gzipInputStream, byteOutputStream); return byteOutputStream.toByteArray(); }
private void runDebugCommand(List<?> nonOptionArgs) throws Exception { logger.log(Level.INFO, "Running 'decrypt' command with arguments: "+nonOptionArgs); if (nonOptionArgs.size() != 1) { throw new Exception("Invalid syntax for 'debug' command. Argument expected."); } if (!isInitializedScope()) { throw new Exception("Command 'debug' can only be run in initialized local dir."); } File decryptFile = new File(nonOptionArgs.get(0).toString()); if (!decryptFile.exists()) { throw new Exception("Given file does not exist: "+decryptFile); } InputStream fileInputStream = config.getTransformer().createInputStream(new FileInputStream(decryptFile)); IOUtils.copy(fileInputStream, System.out); System.exit(0); }
@Override public byte[] inflate(InputStream data) throws CompressionException { ByteArrayOutputStream out; GZIPInputStream gzip = null; try { out = new ByteArrayOutputStream(); gzip = new GZIPInputStream(data); IOUtils.copy(gzip, out); } catch (IOException e) { throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName(), e); } finally { if (gzip != null) { IOUtils.closeQuietly(gzip); } } return out.toByteArray(); }
protected static boolean matchChecksum(File localFile, String expectedSignature) { try (FileInputStream input = new FileInputStream(localFile)) { MessageDigest digester = MessageDigest.getInstance("MD5"); try (DigestInputStream digest = new DigestInputStream(input, digester)) { IOUtils.copy(digest, new NullOutputStream()); } return expectedSignature.equalsIgnoreCase(encodeHexString(digester.digest())); } catch (Exception e) { throw new RuntimeException(e); } }