List of usage examples for org.apache.commons.compress.archivers.cpio CpioArchiveEntry CpioArchiveEntry
public CpioArchiveEntry(File inputFile, String entryName)
From source file:com.espringtran.compressor4j.processor.CpioProcessor.java
/** * Compress data//from www. j ava 2 s . c o m * * @param fileCompressor * FileCompressor object * @return * @throws Exception */ @Override public byte[] compressData(FileCompressor fileCompressor) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); CpioArchiveOutputStream aos = new CpioArchiveOutputStream(baos); try { for (BinaryFile binaryFile : fileCompressor.getMapBinaryFile().values()) { CpioArchiveEntry entry = new CpioArchiveEntry(binaryFile.getDesPath(), binaryFile.getActualSize()); aos.putArchiveEntry(entry); aos.write(binaryFile.getData()); aos.closeArchiveEntry(); } aos.flush(); aos.finish(); } catch (Exception e) { FileCompressor.LOGGER.error("Error on compress data", e); } finally { aos.close(); baos.close(); } return baos.toByteArray(); }
From source file:org.dataconservancy.packaging.tool.impl.generator.BagItPackageAssembler.java
private void addFilesToArchive(ArchiveOutputStream taos, File file) throws IOException { // Create an entry for the file //taos.putArchiveEntry(new TarArchiveEntry(file, file.getParentFile().toURI().relativize(file.toURI()).toString())); switch (archivingFormat) { case ArchiveStreamFactory.TAR: taos.putArchiveEntry(new TarArchiveEntry(file, FilenameUtils.separatorsToUnix( Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString()))); break;/* w ww . j a v a 2s . c om*/ case ArchiveStreamFactory.ZIP: taos.putArchiveEntry(new ZipArchiveEntry(file, FilenameUtils.separatorsToUnix( Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString()))); break; case ArchiveStreamFactory.JAR: taos.putArchiveEntry(new JarArchiveEntry(new ZipArchiveEntry(file, FilenameUtils.separatorsToUnix( Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString())))); break; case ArchiveStreamFactory.AR: taos.putArchiveEntry(new ArArchiveEntry(file, FilenameUtils.separatorsToUnix( Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString()))); break; case ArchiveStreamFactory.CPIO: taos.putArchiveEntry(new CpioArchiveEntry(file, FilenameUtils.separatorsToUnix( Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString()))); break; } if (file.isFile()) { // Add the file to the archive BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); IOUtils.copy(bis, taos); taos.closeArchiveEntry(); bis.close(); } else if (file.isDirectory()) { // close the archive entry taos.closeArchiveEntry(); // go through all the files in the directory and using recursion, add them to the archive for (File childFile : file.listFiles()) { addFilesToArchive(taos, childFile); } } }