List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveOutputStream write
public void write(byte[] wBuf, int wOffset, int numToWrite) throws IOException
From source file:org.apache.camel.processor.aggregate.tarfile.TarAggregationStrategy.java
private static void addEntryToTar(File source, String entryName, byte[] buffer, int length) throws IOException, ArchiveException { File tmpTar = File.createTempFile(source.getName(), null); tmpTar.delete();//from ww w. ja v a2s.c om if (!source.renameTo(tmpTar)) { throw new IOException("Cannot create temp file: " + source.getName()); } TarArchiveInputStream tin = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream(ArchiveStreamFactory.TAR, new FileInputStream(tmpTar)); TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(source)); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX); tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX); // copy the existing entries ArchiveEntry nextEntry; while ((nextEntry = tin.getNextEntry()) != null) { tos.putArchiveEntry(nextEntry); IOUtils.copy(tin, tos); tos.closeArchiveEntry(); } // Create new entry TarArchiveEntry entry = new TarArchiveEntry(entryName); entry.setSize(length); tos.putArchiveEntry(entry); tos.write(buffer, 0, length); tos.closeArchiveEntry(); IOHelper.close(tin); IOHelper.close(tos); }
From source file:org.apache.nifi.util.FlowFilePackagerV1.java
private void writeContentEntry(final TarArchiveOutputStream tarOut, final InputStream inStream, final long fileSize) throws IOException { final TarArchiveEntry entry = new TarArchiveEntry(FILENAME_CONTENT); entry.setMode(tarPermissions);// www. j a v a 2s . com entry.setSize(fileSize); tarOut.putArchiveEntry(entry); final byte[] buffer = new byte[512 << 10];//512KB int bytesRead = 0; while ((bytesRead = inStream.read(buffer)) != -1) { //still more data to read if (bytesRead > 0) { tarOut.write(buffer, 0, bytesRead); } } copy(inStream, tarOut); tarOut.closeArchiveEntry(); }
From source file:org.eclipse.orion.server.docker.server.DockerFile.java
/** * Get the tar file containing the Dockerfile that can be sent to the Docker * build API to create an image. //from w ww . j av a 2 s.co m * * @return The tar file. */ public File getTarFile() { try { // get the temporary folder location. String tmpDirName = System.getProperty("java.io.tmpdir"); File tmpDir = new File(tmpDirName); if (!tmpDir.exists() || !tmpDir.isDirectory()) { if (logger.isDebugEnabled()) { logger.error("Cannot find the default temporary-file directory: " + tmpDirName); } return null; } // get a temporary folder name long n = random.nextLong(); n = (n == Long.MIN_VALUE) ? 0 : Math.abs(n); String tmpDirStr = Long.toString(n); tempFolder = new File(tmpDir, tmpDirStr); if (!tempFolder.mkdir()) { if (logger.isDebugEnabled()) { logger.error("Cannot create a temporary directory at " + tempFolder.toString()); } return null; } if (logger.isDebugEnabled()) { logger.debug("Dockerfile: Created a temporary directory at " + tempFolder.toString()); } // create the Dockerfile dockerfile = new File(tempFolder, "Dockerfile"); FileOutputStream fileOutputStream = new FileOutputStream(dockerfile); Charset utf8 = Charset.forName("UTF-8"); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, utf8); outputStreamWriter.write(getDockerfileContent()); outputStreamWriter.flush(); outputStreamWriter.close(); fileOutputStream.close(); dockerTarFile = new File(tempFolder, "Dockerfile.tar"); TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream( new FileOutputStream(dockerTarFile)); tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); TarArchiveEntry tarEntry = new TarArchiveEntry(dockerfile); tarEntry.setName(dockerfile.getName()); tarArchiveOutputStream.putArchiveEntry(tarEntry); FileInputStream fileInputStream = new FileInputStream(dockerfile); BufferedInputStream inputStream = new BufferedInputStream(fileInputStream); byte[] buffer = new byte[4096]; int bytes_read; while ((bytes_read = inputStream.read(buffer)) != -1) { tarArchiveOutputStream.write(buffer, 0, bytes_read); } inputStream.close(); tarArchiveOutputStream.closeArchiveEntry(); tarArchiveOutputStream.close(); if (logger.isDebugEnabled()) { logger.debug("Dockerfile: Created a docker tar file at " + dockerTarFile.toString()); } return dockerTarFile; } catch (IOException e) { logger.error(e.getLocalizedMessage(), e); } return null; }
From source file:org.eclipse.tracecompass.integration.swtbot.tests.projectexplorer.TestDirectoryStructureUtil.java
private static void addToArchive(TarArchiveOutputStream taos, File dir, File root) throws IOException { byte[] buffer = new byte[1024]; int length;/*from w w w . ja va2 s . co m*/ int index = root.getAbsolutePath().length(); for (File file : dir.listFiles()) { String name = file.getAbsolutePath().substring(index); if (file.isDirectory()) { if (file.listFiles().length != 0) { addToArchive(taos, file, root); } else { taos.putArchiveEntry(new TarArchiveEntry(name + File.separator)); taos.closeArchiveEntry(); } } else { try (FileInputStream fis = new FileInputStream(file)) { TarArchiveEntry entry = new TarArchiveEntry(name); entry.setSize(file.length()); taos.putArchiveEntry(entry); while ((length = fis.read(buffer)) > 0) { taos.write(buffer, 0, length); } taos.closeArchiveEntry(); } } } }
From source file:org.jclouds.docker.compute.features.internal.Archives.java
public static File tar(File baseDir, String archivePath) throws IOException { // Check that the directory is a directory, and get its contents checkArgument(baseDir.isDirectory(), "%s is not a directory", baseDir); File[] files = baseDir.listFiles(); File tarFile = new File(archivePath); String token = getLast(Splitter.on("/").split(archivePath.substring(0, archivePath.lastIndexOf("/")))); byte[] buf = new byte[1024]; int len;// w w w . j ava2s . c om TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile)); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (File file : files) { TarArchiveEntry tarEntry = new TarArchiveEntry(file); tarEntry.setName("/" + getLast(Splitter.on(token).split(file.toString()))); tos.putArchiveEntry(tarEntry); if (!file.isDirectory()) { FileInputStream fin = new FileInputStream(file); BufferedInputStream in = new BufferedInputStream(fin); while ((len = in.read(buf)) != -1) { tos.write(buf, 0, len); } in.close(); } tos.closeArchiveEntry(); } tos.close(); return tarFile; }
From source file:org.jclouds.docker.compute.features.internal.Archives.java
public static File tar(File baseDir, File tarFile) throws IOException { // Check that the directory is a directory, and get its contents checkArgument(baseDir.isDirectory(), "%s is not a directory", baseDir); File[] files = baseDir.listFiles(); String token = getLast(Splitter.on("/").split(baseDir.getAbsolutePath())); byte[] buf = new byte[1024]; int len;/*from ww w. jav a2s. c o m*/ TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile)); tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); for (File file : files) { TarArchiveEntry tarEntry = new TarArchiveEntry(file); tarEntry.setName("/" + getLast(Splitter.on(token).split(file.toString()))); tos.putArchiveEntry(tarEntry); if (!file.isDirectory()) { FileInputStream fin = new FileInputStream(file); BufferedInputStream in = new BufferedInputStream(fin); while ((len = in.read(buf)) != -1) { tos.write(buf, 0, len); } in.close(); } tos.closeArchiveEntry(); } tos.close(); return tarFile; }
From source file:org.kitesdk.cli.commands.TestTarImportCommand.java
private static void writeToTarFile(TarArchiveOutputStream tos, String name, String content) throws IOException { TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(name); if (null != content) { tarArchiveEntry.setSize(content.length()); }//from w ww . j av a 2 s.c o m tarArchiveEntry.setModTime(System.currentTimeMillis()); tos.putArchiveEntry(tarArchiveEntry); if (null != content) { byte[] buf = content.getBytes(); tos.write(buf, 0, content.length()); tos.flush(); } tos.closeArchiveEntry(); }
From source file:org.opentestsystem.delivery.testreg.transformer.TarBundler.java
/** * Bundles the inputs into a tar which is returned as a byte stream. The input is an array of byte arrays in which * the first element is a filename and the second element is the actual file. Subsequent files follow this same * pattern. If there is an uneven number of elements then the last file will be ignored. * /*from w ww. j a v a 2s. c o m*/ * @param inputs * @return */ @Transformer public Message<File> bundleToTar(final String[] inputs, final @Header("dwBatchUuid") String dwBatchUuid, final @Header("fileSuffix") String fileSuffix, final @Header("recordsSent") int recordsSent, final @Header("tempPaths") List<Path> tempPaths, final @Header("dwConfigType") DwConfigType dwConfigType) { String debugPrefix = dwConfigType + " DW Config: "; long curTime = System.currentTimeMillis(); File tmpTarFile; FileOutputStream tmpTarOutStream; FileInputStream tmpCsvInStream; FileInputStream tmpJsonInStream; try { Path tmpTarPath = Files.createTempFile(DwBatchHandler.DW_TAR_TMP_PREFIX, (dwConfigType == DwConfigType.SBAC ? DwBatchHandler.SBAC_DW_NAME : DwBatchHandler.LOCAL_DW_NAME) + fileSuffix); tempPaths.add(tmpTarPath); tmpTarFile = tmpTarPath.toFile(); LOGGER.debug(debugPrefix + "Created temp TAR file " + tmpTarFile.getAbsolutePath()); tmpTarOutStream = new FileOutputStream(tmpTarFile); tmpCsvInStream = new FileInputStream(inputs[1]); tmpJsonInStream = new FileInputStream(inputs[4]); TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(tmpTarOutStream); String csvFilename = inputs[0]; String jsonFilename = inputs[3]; // tar archive entry for the csv file TarArchiveEntry entry = new TarArchiveEntry(csvFilename); entry.setSize(Integer.valueOf(inputs[2])); tarOutput.putArchiveEntry(entry); byte[] buf = new byte[BUFFER_SIZE]; int len; while ((len = tmpCsvInStream.read(buf)) > 0) { tarOutput.write(buf, 0, len); } tarOutput.closeArchiveEntry(); // tar archive entry for the json file entry = new TarArchiveEntry(jsonFilename); entry.setSize(Integer.valueOf(inputs[5])); tarOutput.putArchiveEntry(entry); buf = new byte[BUFFER_SIZE]; while ((len = tmpJsonInStream.read(buf)) > 0) { tarOutput.write(buf, 0, len); } tarOutput.closeArchiveEntry(); tarOutput.close(); tmpCsvInStream.close(); tmpJsonInStream.close(); } catch (IOException e) { throw new TarBundlerException(debugPrefix + "failure to tar output streams", e); } LOGGER.debug(debugPrefix + "Created TAR output in " + (System.currentTimeMillis() - curTime)); return MessageBuilder.withPayload(tmpTarFile).setHeader("dwBatchUuid", dwBatchUuid) .setHeader("fileSuffix", fileSuffix).setHeader("recordsSent", recordsSent) .setHeader("tempPaths", tempPaths).setHeader("dwConfigType", dwConfigType).build(); }