本文整理汇总了Java中org.apache.commons.compress.archivers.ArchiveOutputStream类的典型用法代码示例。如果您正苦于以下问题:Java ArchiveOutputStream类的具体用法?Java ArchiveOutputStream怎么用?Java ArchiveOutputStream使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ArchiveOutputStream类属于org.apache.commons.compress.archivers包,在下文中一共展示了ArchiveOutputStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testApache
import org.apache.commons.compress.archivers.ArchiveOutputStream; //导入依赖的package包/类
public void testApache() throws IOException, ArchiveException {
log.debug("testApache()");
File zip = File.createTempFile("apache_", ".zip");
// Create zip
FileOutputStream fos = new FileOutputStream(zip);
ArchiveOutputStream aos = new ArchiveStreamFactory().createArchiveOutputStream("zip", fos);
aos.putArchiveEntry(new ZipArchiveEntry("coñeta"));
aos.closeArchiveEntry();
aos.close();
// Read zip
FileInputStream fis = new FileInputStream(zip);
ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream("zip", fis);
ZipArchiveEntry zae = (ZipArchiveEntry) ais.getNextEntry();
assertEquals(zae.getName(), "coñeta");
ais.close();
}
示例2: copyArchiveContents
import org.apache.commons.compress.archivers.ArchiveOutputStream; //导入依赖的package包/类
/**
* Copies the contents of an archive file to the specified output stream
*
* @param archiveFile the archive file
* @param outputStream the output stream
* @throws IOException error copying the archive contents
*/
private void copyArchiveContents(File archiveFile, ArchiveOutputStream outputStream) throws IOException {
ArchiveInputStream inputStream = this.createArchiveInputStream(archiveFile);
ArchiveEntry entry;
byte[] buffer = new byte[this.bufferSize];
while ((entry = inputStream.getNextEntry()) != null) {
outputStream.putArchiveEntry(entry);
int count;
while ((count = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, count);
}
outputStream.closeArchiveEntry();
}
}
示例3: addTemplateFiles
import org.apache.commons.compress.archivers.ArchiveOutputStream; //导入依赖的package包/类
private void addTemplateFiles(ArchiveOutputStream archive) throws IOException {
// TODO: maybe get the template path from config?
Path templateDirectory = FileSystems.getDefault().getPath("tmc-assets", "submission-template");
List<Path> templateEntries = getDirectoryEntries(templateDirectory);
for (Path entry : templateEntries) {
// Paths must be relativized against the template directory root
// before including them into the Tar archive.
// I.e. a file at ./tmc-assets/submission-template/directory/file.txt will be added to
// the archive as ./directory/file.txt.
String tarEntryName = templateDirectory.relativize(entry).toString();
// TODO: special case for build.xml for renaming the project.
addFile(archive, tarEntryName, entry);
}
}
示例4: packageSubmission
import org.apache.commons.compress.archivers.ArchiveOutputStream; //导入依赖的package包/类
/**
* @param additionalFiles A map of filename -> content pairs to include in the package.
* @return The packaged submission as a TAR archive.
*/
@Override
public byte[] packageSubmission(Map<String, byte[]> additionalFiles)
throws IOException, ArchiveException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (ArchiveOutputStream archive = new ArchiveStreamFactory()
.createArchiveOutputStream(ArchiveStreamFactory.TAR, outputStream)) {
addTemplateFiles(archive);
addAdditionalFiles(archive, additionalFiles);
archive.finish();
}
logger.debug("Finished packaging submission, size {} bytes.", outputStream.size());
return outputStream.toByteArray();
}
示例5: makeOnlyZip
import org.apache.commons.compress.archivers.ArchiveOutputStream; //导入依赖的package包/类
public static void makeOnlyZip() throws IOException, ArchiveException{
File f1 = new File("D:/compresstest.txt");
File f2 = new File("D:/test1.xml");
final OutputStream out = new FileOutputStream("D:/中文名字.zip");
ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, out);
os.putArchiveEntry(new ZipArchiveEntry(f1.getName()));
IOUtils.copy(new FileInputStream(f1), os);
os.closeArchiveEntry();
os.putArchiveEntry(new ZipArchiveEntry(f2.getName()));
IOUtils.copy(new FileInputStream(f2), os);
os.closeArchiveEntry();
os.close();
}
示例6: addFilesToZip
import org.apache.commons.compress.archivers.ArchiveOutputStream; //导入依赖的package包/类
void addFilesToZip(File source, File destination) throws IOException, ArchiveException {
OutputStream archiveStream = new FileOutputStream(destination);
ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, archiveStream);
Collection<File> fileList = FileUtils.listFiles(source, null, true);
for (File file : fileList) {
String entryName = getEntryName(source, file);
ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
archive.putArchiveEntry(entry);
BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
IOUtils.copy(input, archive);
input.close();
archive.closeArchiveEntry();
}
archive.finish();
archiveStream.close();
}
示例7: compressDirectory
import org.apache.commons.compress.archivers.ArchiveOutputStream; //导入依赖的package包/类
public static void compressDirectory(File rootDir, boolean includeAsRoot, File output) throws IOException {
if (!rootDir.isDirectory()) {
throw new IOException("Provided file is not a directory");
}
try (OutputStream archiveStream = new FileOutputStream(output);
ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, archiveStream)) {
String rootName = "";
if (includeAsRoot) {
insertDirectory(rootDir, rootDir.getName(), archive);
rootName = rootDir.getName()+"/";
}
Collection<File> fileCollection = FileUtils.listFilesAndDirs(rootDir, TrueFileFilter.TRUE, TrueFileFilter.TRUE);
for (File file : fileCollection) {
String relativePath = getRelativePath(rootDir, file);
String entryName = rootName+relativePath;
if (!relativePath.isEmpty() && !"/".equals(relativePath)) {
insertObject(file, entryName, archive);
}
}
archive.finish();
} catch (IOException | ArchiveException e) {
throw new IOException(e);
}
}
示例8: insertFile
import org.apache.commons.compress.archivers.ArchiveOutputStream; //导入依赖的package包/类
private static void insertFile(File file, String entryName, ArchiveOutputStream archive) throws IOException {
if (DEFAULT_EXCLUDES.contains(file.getName())) {
logger.debug("Skipping ! [l] {}", entryName);
return;
}
if (file.isFile()) {
ZipArchiveEntry newEntry = new ZipArchiveEntry(entryName);
setExtraFields(file.toPath(), UnixStat.FILE_FLAG, newEntry);
archive.putArchiveEntry(newEntry);
BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
IOUtils.copy(input, archive);
input.close();
archive.closeArchiveEntry();
} else {
throw new IOException("Provided file is not a file");
}
}
示例9: getArchiveInputStream
import org.apache.commons.compress.archivers.ArchiveOutputStream; //导入依赖的package包/类
private InputStream getArchiveInputStream(String format, String ...resourceFiles)
throws ArchiveException, URISyntaxException, IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ArchiveStreamFactory factory = new ArchiveStreamFactory();
ArchiveOutputStream aout = factory.createArchiveOutputStream(format, bout);
for (String resource : resourceFiles) {
File f = new File(getClass().getResource(resource).toURI());
ArchiveEntry entry = aout.createArchiveEntry(f, resource);
aout.putArchiveEntry(entry);
IOUtils.copy(new FileInputStream(f),aout);
aout.closeArchiveEntry();
}
aout.finish();
aout.close();
return new ByteArrayInputStream(bout.toByteArray());
}
示例10: compressArchive
import org.apache.commons.compress.archivers.ArchiveOutputStream; //导入依赖的package包/类
private static void compressArchive(
final Path pathToCompress,
final ArchiveOutputStream archiveOutputStream,
final ArchiveEntryFactory archiveEntryFactory,
final CompressionType compressionType,
final BuildListener listener)
throws IOException {
final List<File> files = addFilesToCompress(pathToCompress, listener);
LoggingHelper.log(listener, "Compressing directory '%s' as a '%s' archive",
pathToCompress.toString(),
compressionType.name());
for (final File file : files) {
final String newTarFileName = pathToCompress.relativize(file.toPath()).toString();
final ArchiveEntry archiveEntry = archiveEntryFactory.create(file, newTarFileName);
archiveOutputStream.putArchiveEntry(archiveEntry);
try (final FileInputStream fileInputStream = new FileInputStream(file)) {
IOUtils.copy(fileInputStream, archiveOutputStream);
}
archiveOutputStream.closeArchiveEntry();
}
}
示例11: createPackageZip
import org.apache.commons.compress.archivers.ArchiveOutputStream; //导入依赖的package包/类
/**
* Creates the {@code CRX} package definition.
*
* @param packageName Name of the package to create.
*/
public final void createPackageZip(final String packageName) {
try {
ArchiveOutputStream archiveOutputStream =
new ZipArchiveOutputStream(new File("src/main/resources/" + packageName + ".zip"));
addZipEntry(archiveOutputStream, "META-INF/vault/definition/.content.xml");
addZipEntry(archiveOutputStream, "META-INF/vault/config.xml");
addZipEntry(archiveOutputStream, "META-INF/vault/filter.xml");
addZipEntry(archiveOutputStream, "META-INF/vault/nodetypes.cnd");
addZipEntry(archiveOutputStream, "META-INF/vault/properties.xml");
addZipEntry(archiveOutputStream, "jcr_root/.content.xml");
archiveOutputStream.finish();
archiveOutputStream.close();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Unable to create package zip: {0}", e.getMessage());
}
}
示例12: addZipEntry
import org.apache.commons.compress.archivers.ArchiveOutputStream; //导入依赖的package包/类
/**
* Adds an entry to the zip.
*
* @param archiveOutputStream {@code ArchiveOutputStream} to output zip entry to.
* @param filename Name of the file to add to the zip.
* @throws IOException If an error occurs adding the file to the zip.
*/
private void addZipEntry(final ArchiveOutputStream archiveOutputStream, final String filename) throws IOException {
FileInputStream fileInputStream = null;
try {
File file = new File("src/main/resources/" + filename);
ArchiveEntry archiveEntry = archiveOutputStream.createArchiveEntry(file, filename);
archiveOutputStream.putArchiveEntry(archiveEntry);
fileInputStream = new FileInputStream(file);
IOUtils.copy(fileInputStream, archiveOutputStream);
archiveOutputStream.closeArchiveEntry();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Unable to create zip entry for file: [{0}]. {1}",
new String[]{filename, e.getMessage()});
} finally {
if (fileInputStream != null) {
fileInputStream.close();
}
}
}
示例13: compress
import org.apache.commons.compress.archivers.ArchiveOutputStream; //导入依赖的package包/类
@Override
public void compress(File[] files, String location, String name)
throws IOException, ArchiveException, CompressorException {
initAlgorithmProgress(files);
String fullname = FileUtils.combinePathAndFilename(location, name);
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fullname))) {
try (CompressorOutputStream cos = makeCompressorOutputStream(bos)) {
try (ArchiveOutputStream aos = cos != null
? makeArchiveOutputStream(cos)
: makeArchiveOutputStream(bos)) {
compress(files, "", aos);
}
}
}
}
示例14: addTargetToArchiveOutputStream
import org.apache.commons.compress.archivers.ArchiveOutputStream; //导入依赖的package包/类
/**
* Add some target to the archive outputstream.
*
* @param target the target to write in the outputstream.
* @param archiveOutputStream the archive outputstream.
* @throws IOException if something goes wrong.
*/
private void addTargetToArchiveOutputStream(Target target, ArchiveOutputStream archiveOutputStream) throws IOException {
Volume targetVolume = target.getVolume();
try (InputStream targetInputStream = targetVolume.openInputStream(target)) {
// get the target infos
final long targetSize = targetVolume.getSize(target);
final byte[] targetContent = new byte[(int) targetSize];
final String targetPath = targetVolume.getPath(target); // relative path
// creates the entry and writes in the archive output stream
ArchiveEntry entry = createArchiveEntry(targetPath, targetSize, targetContent);
archiveOutputStream.putArchiveEntry(entry);
// archiveOutputStream.write(targetContent);
IOUtils.copy(targetInputStream, archiveOutputStream);
archiveOutputStream.closeArchiveEntry();
}
}
示例15: addFilesToZip
import org.apache.commons.compress.archivers.ArchiveOutputStream; //导入依赖的package包/类
@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);
}
}