本文整理汇总了Java中org.apache.commons.compress.compressors.CompressorInputStream类的典型用法代码示例。如果您正苦于以下问题:Java CompressorInputStream类的具体用法?Java CompressorInputStream怎么用?Java CompressorInputStream使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CompressorInputStream类属于org.apache.commons.compress.compressors包,在下文中一共展示了CompressorInputStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createCompressorInputStream
import org.apache.commons.compress.compressors.CompressorInputStream; //导入依赖的package包/类
CompressorInputStream createCompressorInputStream(String format,
InputStream in) throws IOException, CompressorException {
CompressorStreamFactory factory = new CompressorStreamFactory();
factory.setDecompressConcatenated(decompressConcatenated);
if (CommonsCompressUtil.isAutoDetect(format)) {
in = in.markSupported() ? in : new BufferedInputStream(in);
try {
return factory.createCompressorInputStream(in);
} catch (CompressorException e) {
throw new IOException(
"Failed to detect a file format. Please try to set a format explicitly.",
e);
}
} else {
return factory.createCompressorInputStream(format, in);
}
}
示例2: shouldStreamGzipCompressedFile
import org.apache.commons.compress.compressors.CompressorInputStream; //导入依赖的package包/类
@Test
public void shouldStreamGzipCompressedFile() throws Exception {
String line;
final File logFile = new File(ACCESS_LOG_GZIP_FILE);
final FileInputStream fileInputStream = new FileInputStream(logFile);
final BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
final CompressorInputStream compressedInputStream = new CompressorStreamFactory().createCompressorInputStream(bufferedInputStream);
final InputStreamReader inputStreamReader = new InputStreamReader(compressedInputStream);
final BufferedReader reader = new BufferedReader(inputStreamReader);
while ((line = reader.readLine()) != null) {
assertTrue(line.contains("localhost"));
}
reader.close();
}
示例3: shouldStreamBzip2CompressedFile
import org.apache.commons.compress.compressors.CompressorInputStream; //导入依赖的package包/类
@Test
public void shouldStreamBzip2CompressedFile() throws Exception {
String line;
final File logFile = new File(ACCESS_LOG_BZIP2_FILE);
final FileInputStream fileInputStream = new FileInputStream(logFile);
final BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
final CompressorInputStream compressedInputStream = new CompressorStreamFactory().createCompressorInputStream(bufferedInputStream);
final InputStreamReader inputStreamReader = new InputStreamReader(compressedInputStream);
final BufferedReader reader = new BufferedReader(inputStreamReader);
while ((line = reader.readLine()) != null) {
assertTrue(line.contains("localhost"));
}
reader.close();
}
示例4: decompress
import org.apache.commons.compress.compressors.CompressorInputStream; //导入依赖的package包/类
@Override
public byte[] decompress(ByteArrayInputStream bais) throws Exception {
ByteArrayOutputStream baos=new ByteArrayOutputStream();
CompressorInputStream cis=FACTORY.createCompressorInputStream(name, bais);
int len;
byte buf[]=new byte[BUFFER];
while((len=cis.read(buf, 0, BUFFER)) != -1) {
baos.write(buf, 0, len);
}
cis.close();
byte[] output=baos.toByteArray();
baos.flush();
baos.close();
bais.close();
return output;
}
示例5: unCompress
import org.apache.commons.compress.compressors.CompressorInputStream; //导入依赖的package包/类
/** Unpackages single file content, uses various techniques to uncompress it */
public static byte[] unCompress(byte[] content) throws IOException {
//TODO: should probably catch the case if the file is already uncompressed
try (
ByteArrayInputStream compressedIn = new ByteArrayInputStream(content);
CompressorInputStream compressedInStream = new CompressorStreamFactory().createCompressorInputStream(compressedIn);
ByteArrayOutputStream out = new ByteArrayOutputStream();
) {
final byte[] buffer = new byte[8192];
int n;
while ((n = compressedInStream.read(buffer)) != -1) {
out.write(buffer, 0, n);
}
byte[] uncompressed = out.toByteArray();
return uncompressed;
} catch (CompressorException e) {
// System.out.println("General gunzip approach failed trying unzip");
// e.printStackTrace();
// try unzip approach, given the other ones failed
return unZip(content);
}
}
示例6: getArchive
import org.apache.commons.compress.compressors.CompressorInputStream; //导入依赖的package包/类
private static Path getArchive(final Path path) throws IOException {
final Path result;
// Get the extension
final String fileName = path.getFileName().toString();
final String loweredFileName = fileName.toLowerCase(Locale.ENGLISH);
if (loweredFileName.endsWith(".gz")) {
String tempFileName = fileName.substring(0, loweredFileName.indexOf(".gz"));
final int index = tempFileName.lastIndexOf('.');
if (index > 0) {
result = Files.createTempFile(tempFileName.substring(0, index), tempFileName.substring(index, tempFileName.length()));
} else {
result = Files.createTempFile(tempFileName.substring(0, index), "");
}
try (CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream(new BufferedInputStream(Files.newInputStream(path)))) {
Files.copy(in, result, StandardCopyOption.REPLACE_EXISTING);
} catch (CompressorException e) {
throw new IOException(e);
}
} else {
result = path;
}
return result;
}
示例7: decompress
import org.apache.commons.compress.compressors.CompressorInputStream; //导入依赖的package包/类
@Override
public void decompress(File source, File destination) throws IOException {
assertSource(source);
assertDestination(destination);
if (destination.isDirectory()) {
destination = new File(destination, getDecompressedFilename(source));
}
CompressorInputStream compressed = null;
FileOutputStream output = null;
try {
compressed = createCompressorInputStream(getCompressionType(), source);
output = new FileOutputStream(destination);
IOUtils.copy(compressed, output);
} catch (CompressorException e) {
throw new IOException(e);
} finally {
IOUtils.closeQuietly(compressed);
IOUtils.closeQuietly(output);
}
}
示例8: createCompressorInputStream
import org.apache.commons.compress.compressors.CompressorInputStream; //导入依赖的package包/类
@Override
public CompressorInputStream createCompressorInputStream(
final String fileName, final InputStream inputStream)
throws CompressorException {
checkNotNull(inputStream, "inputStream");
checkNotNull(fileName, "fileName");
try {
if (GZIP.equalsIgnoreCase(fileName)) {
return createGZipCompressorInputStream(inputStream);
} else if (BZIP2.equalsIgnoreCase(fileName)) {
return createBZip2CompressorInputStream(inputStream);
} else if (XZ.equalsIgnoreCase(fileName)) {
return createXZCompressorInputStream(inputStream);
} else if (PACK200.equalsIgnoreCase(fileName)) {
return createPack200CompressorInputStream(inputStream);
} else if (nameDetectionFallback) {
return createCompressorInputStream(inputStream);
}
} catch (IOException e) {
throw new CompressorException("Could not create CompressorInputStream.", e);
}
throw new CompressorException("Compressor: " + fileName + " not found.");
}
示例9: testDecompressBZip2
import org.apache.commons.compress.compressors.CompressorInputStream; //导入依赖的package包/类
@Test
public void testDecompressBZip2() throws IOException, CompressorException {
final File outputFile = new File(OUTPUT_PATH, BZIP2_FILE.getName() + ".decompressed");
final CompressorStreamFactory2 instance = CompressorStreamFactory2.builder().build();
final Closer closer = Closer.create();
try {
final CompressorInputStream cis =
closer.register(instance.createCompressorInputStream(
closer.register(new BufferedInputStream(
closer.register(new FileInputStream(BZIP2_FILE))))));
final OutputStream os = closer.register(new BufferedOutputStream(
closer.register(new FileOutputStream(outputFile))));
ByteStreams.copy(cis, os);
} catch (Throwable t) {
throw closer.rethrow(t);
} finally {
closer.close();
}
assertTrue("Output files differ.", Files.equal(outputFile, TEXT_FILE));
}
示例10: testDecompressBZip2_Concatenate
import org.apache.commons.compress.compressors.CompressorInputStream; //导入依赖的package包/类
@Test
public void testDecompressBZip2_Concatenate() throws IOException, CompressorException {
final File outputFile = new File(OUTPUT_PATH, BZIP2_FILE.getName() + ".decompressed");
final CompressorStreamFactory2 instance = CompressorStreamFactory2.builder()
.setDecompressConcatenated(true).build();
final Closer closer = Closer.create();
try {
final CompressorInputStream cis =
closer.register(instance.createCompressorInputStream(
closer.register(new BufferedInputStream(
closer.register(new FileInputStream(BZIP2_FILE))))));
final OutputStream os = closer.register(new BufferedOutputStream(
closer.register(new FileOutputStream(outputFile))));
ByteStreams.copy(cis, os);
} catch (Throwable t) {
throw closer.rethrow(t);
} finally {
closer.close();
}
assertTrue("Output files differ.", Files.equal(outputFile, TEXT_FILE));
}
示例11: testDecompressGZip
import org.apache.commons.compress.compressors.CompressorInputStream; //导入依赖的package包/类
@Test
public void testDecompressGZip() throws IOException, CompressorException {
final File outputFile = new File(OUTPUT_PATH, GZIP_FILE.getName() + ".decompressed");
final CompressorStreamFactory2 instance = CompressorStreamFactory2.builder().build();
final Closer closer = Closer.create();
try {
final CompressorInputStream cis =
closer.register(instance.createCompressorInputStream(
closer.register(new BufferedInputStream(
closer.register(new FileInputStream(GZIP_FILE))))));
final OutputStream os = closer.register(new BufferedOutputStream(
closer.register(new FileOutputStream(outputFile))));
ByteStreams.copy(cis, os);
} catch (Throwable t) {
throw closer.rethrow(t);
} finally {
closer.close();
}
assertTrue("Output files differ.", Files.equal(outputFile, TEXT_FILE));
}
示例12: testDecompressXZ
import org.apache.commons.compress.compressors.CompressorInputStream; //导入依赖的package包/类
@Test
public void testDecompressXZ() throws IOException, CompressorException {
final File outputFile = new File(OUTPUT_PATH, XZ_FILE.getName() + ".decompressed");
final CompressorStreamFactory2 instance = CompressorStreamFactory2.builder().build();
final Closer closer = Closer.create();
try {
final CompressorInputStream cis =
closer.register(instance.createCompressorInputStream(
closer.register(new BufferedInputStream(
closer.register(new FileInputStream(XZ_FILE))))));
final OutputStream os = closer.register(new BufferedOutputStream(
closer.register(new FileOutputStream(outputFile))));
ByteStreams.copy(cis, os);
} catch (Throwable t) {
throw closer.rethrow(t);
} finally {
closer.close();
}
assertTrue("Output files differ.", Files.equal(outputFile, TEXT_FILE));
}
示例13: indexInputFile
import org.apache.commons.compress.compressors.CompressorInputStream; //导入依赖的package包/类
private void indexInputFile() throws IOException, CompressorException {
FileInputStream fin = new FileInputStream(input);
BufferedInputStream in = new BufferedInputStream(fin);
try (CompressorInputStream input = new CompressorStreamFactory().createCompressorInputStream(in)) {
final byte[] buffer = new byte[8192];
int n;
while ((n = input.read(buffer)) != -1) {
String buf = new String(buffer, 0, n); // TODO: not always correct, we need to wait for line end first?
String[] lines = buf.split("\n");
indexLine(lines);
}
}
writeToDisk(1, unigramToCount);
writeToDisk(2, bigramToCount);
writeToDisk(3, trigramToCount);
}
示例14: extract
import org.apache.commons.compress.compressors.CompressorInputStream; //导入依赖的package包/类
private void extract(Language language, String xmlDumpPath) throws IOException, CompressorException {
try (FileInputStream fis = new FileInputStream(xmlDumpPath);
BufferedInputStream bis = new BufferedInputStream(fis);
CompressorInputStream input = new CompressorStreamFactory().createCompressorInputStream(bis)) {
int sentenceCount = 0;
WikipediaSentenceSource source = new WikipediaSentenceSource(input, language);
while (source.hasNext()) {
String sentence = source.next().getText();
if (skipSentence(sentence)) {
continue;
}
System.out.println(sentence);
sentenceCount++;
if (sentenceCount % 1000 == 0) {
System.err.println("Exporting sentence #" + sentenceCount + "...");
}
}
}
}
示例15: getReadableChannel
import org.apache.commons.compress.compressors.CompressorInputStream; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public synchronized ReadableByteChannel getReadableChannel() throws ContentIOException
{
this.ensureDelegate();
final String mimetype = this.getMimetype();
final boolean shouldCompress = this.mimetypesToCompress == null || this.mimetypesToCompress.isEmpty()
|| (mimetype != null && (this.mimetypesToCompress.contains(mimetype) || this.isMimetypeToCompressWildcardMatch(mimetype)));
ReadableByteChannel channel;
if (shouldCompress)
{
LOGGER.debug("Content will be decompressed from backing store (url={})", this.getContentUrl());
final String compressiongType = this.compressionType != null && !this.compressionType.trim().isEmpty() ? this.compressionType
: CompressorStreamFactory.GZIP;
try
{
final CompressorInputStream is = COMPRESSOR_STREAM_FACTORY.createCompressorInputStream(compressiongType,
this.delegate.getContentInputStream());
channel = Channels.newChannel(is);
}
catch (final CompressorException e)
{
LOGGER.error("Failed to open decompressing channel", e);
throw new ContentIOException("Failed to open channel: " + this, e);
}
}
else
{
LOGGER.debug("Content will not be decompressed from backing store (url={})", this.getContentUrl());
channel = super.getReadableChannel();
}
return channel;
}