本文整理汇总了Java中org.apache.tools.bzip2.CBZip2InputStream类的典型用法代码示例。如果您正苦于以下问题:Java CBZip2InputStream类的具体用法?Java CBZip2InputStream怎么用?Java CBZip2InputStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CBZip2InputStream类属于org.apache.tools.bzip2包,在下文中一共展示了CBZip2InputStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: bunzip2
import org.apache.tools.bzip2.CBZip2InputStream; //导入依赖的package包/类
/**
* Uncompresses a BZIP2 file.
*
* @param bytes The compressed bytes without the header.
* @return The uncompressed bytes.
* @throws IOException if an I/O error occurs.
*/
public static byte[] bunzip2(byte[] bytes) throws IOException {
/* prepare a new byte array with the bzip2 header at the start */
byte[] bzip2 = new byte[bytes.length + 2];
bzip2[0] = 'h';
bzip2[1] = '1';
System.arraycopy(bytes, 0, bzip2, 2, bytes.length);
InputStream is = new CBZip2InputStream(new ByteArrayInputStream(bzip2));
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
byte[] buf = new byte[4096];
int len = 0;
while ((len = is.read(buf, 0, buf.length)) != -1) {
os.write(buf, 0, len);
}
} finally {
os.close();
}
return os.toByteArray();
} finally {
is.close();
}
}
示例2: bunzip2
import org.apache.tools.bzip2.CBZip2InputStream; //导入依赖的package包/类
/**
* Uncompresses a BZIP2 file.
*
* @param bytes
* The compressed bytes without the header.
* @return The uncompressed bytes.
* @throws IOException
* if an I/O error occurs.
*/
public static byte[] bunzip2(byte[] bytes) throws IOException {
/* prepare a new byte array with the bzip2 header at the start */
byte[] bzip2 = new byte[bytes.length + 2];
bzip2[0] = 'h';
bzip2[1] = '1';
System.arraycopy(bytes, 0, bzip2, 2, bytes.length);
InputStream is = new CBZip2InputStream(new ByteArrayInputStream(bzip2));
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
byte[] buf = new byte[4096];
int len = 0;
while ((len = is.read(buf, 0, buf.length)) != -1) {
os.write(buf, 0, len);
}
} finally {
os.close();
}
return os.toByteArray();
} finally {
is.close();
}
}
示例3: openBzipFileForReading
import org.apache.tools.bzip2.CBZip2InputStream; //导入依赖的package包/类
/**
* Opens a GZIP-encoded file for reading, decompressing it if necessary
*
* @param file The file to open
* @return the input stream to read from
*/
public static InputStream openBzipFileForReading(final File file) {
try {
final FileInputStream fis = new FileInputStream(file);
if(fis.read() != 66 || fis.read() != 90) { //Read magic number 'BZ' or else CBZip2InputStream will complain about it
fis.close();
throw new PicardException(file.getAbsolutePath() + " is not a BZIP file.");
}
return new CBZip2InputStream(fis);
}
catch (IOException ioe) {
throw new PicardException("Error opening file: " + file.getName(), ioe);
}
}
示例4: extractBzip
import org.apache.tools.bzip2.CBZip2InputStream; //导入依赖的package包/类
private void extractBzip(File downloadFile)
throws IOException
{
try (FileInputStream inputSkipTwo = new FileInputStream(downloadFile)) {
// see javadoc for CBZip2InputStream
// first two bits need to be skipped
inputSkipTwo.read();
inputSkipTwo.read();
LOG.debug("Extract tar...");
try (TarInputStream tarIn = new TarInputStream(new CBZip2InputStream(inputSkipTwo))) {
for (TarEntry entry = tarIn.getNextEntry(); entry != null; entry = tarIn.getNextEntry()) {
LOG.debug("Extracting {}", entry.getName());
File extractedFile = new File(downloadFile.getParent() + File.separator + entry.getName());
extractEntry(extractedFile, entry.isDirectory(), tarIn);
}
}
}
}
示例5: getInputStreamAtOffset
import org.apache.tools.bzip2.CBZip2InputStream; //导入依赖的package包/类
private InputStream getInputStreamAtOffset(final RandomAccessFile file, long offset) throws IOException {
if (offset + 2 >= file.length()) {
throw new IOException("read past EOF");
}
file.seek(offset + 2); // skip past 'BZ' header
InputStream is = new CBZip2InputStream(new FileInputStream(file.getFD()) {
@Override
public void close() throws IOException {
}
});
if (offset == 0) {
return new SequenceInputStream(is, new ByteArrayInputStream(MEDIAWIKI_CLOSING.getBytes()));
} else {
return new SequenceInputStream(
new ByteArrayInputStream(MEDIAWIKI_OPENING.getBytes()),
new SequenceInputStream(is,
new ByteArrayInputStream(MEDIAWIKI_CLOSING.getBytes())));
}
}
示例6: decompress
import org.apache.tools.bzip2.CBZip2InputStream; //导入依赖的package包/类
/**
* This method wraps the input stream with the
* corresponding decompression method
*
* @param name provides location information for BuildException
* @param istream input stream
* @return input stream with on-the-fly decompression
* @exception IOException thrown by GZIPInputStream constructor
* @exception BuildException thrown if bzip stream does not
* start with expected magic values
*/
public InputStream decompress(final String name,
final InputStream istream)
throws IOException, BuildException {
final String v = getValue();
if (GZIP.equals(v)) {
return new GZIPInputStream(istream);
}
if (XZ.equals(v)) {
return newXZInputStream(istream);
}
if (BZIP2.equals(v)) {
final char[] magic = new char[] { 'B', 'Z' };
for (int i = 0; i < magic.length; i++) {
if (istream.read() != magic[i]) {
throw new BuildException("Invalid bz2 file." + name);
}
}
return new CBZip2InputStream(istream);
}
return istream;
}
示例7: bunzip2
import org.apache.tools.bzip2.CBZip2InputStream; //导入依赖的package包/类
/**
* Uncompresses a BZIP2 file.
* @param bytes The compressed bytes without the header.
* @return The uncompressed bytes.
* @throws IOException if an I/O error occurs.
*/
public static byte[] bunzip2(byte[] bytes) throws IOException {
/* prepare a new byte array with the bzip2 header at the start */
byte[] bzip2 = new byte[bytes.length + 2];
bzip2[0] = 'h';
bzip2[1] = '1';
System.arraycopy(bytes, 0, bzip2, 2, bytes.length);
try (InputStream is = new CBZip2InputStream(new ByteArrayInputStream(bzip2))) {
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
byte[] buf = new byte[4096];
int len;
while ((len = is.read(buf, 0, buf.length)) != -1) {
os.write(buf, 0, len);
}
return os.toByteArray();
}
}
}
示例8: tryOpenAsArchive
import org.apache.tools.bzip2.CBZip2InputStream; //导入依赖的package包/类
static public InputStream tryOpenAsArchive (File file, String mimeType, String contentType) {
String fileName = file.getName ();
try {
if (fileName.endsWith (".tar.gz") || fileName.endsWith (".tgz")) {
return new TarInputStream (new GZIPInputStream (new FileInputStream (file)));
} else if (fileName.endsWith (".tar.bz2")) {
return new TarInputStream (new CBZip2InputStream (new FileInputStream (file)));
} else if (fileName.endsWith (".tar") || "application/x-tar".equals (contentType)) {
return new TarInputStream (new FileInputStream (file));
} else if (fileName.endsWith (".zip")
|| "application/x-zip-compressed".equals (contentType)
|| "application/zip".equals (contentType)
|| "application/x-compressed".equals (contentType)
|| "multipar/x-zip".equals (contentType)) {
return new ZipInputStream (new FileInputStream (file));
} else if (fileName.endsWith (".kmz")) {
return new ZipInputStream (new FileInputStream (file));
}
} catch (IOException e) {}
return null;
}
示例9: tryOpenAsCompressedFile
import org.apache.tools.bzip2.CBZip2InputStream; //导入依赖的package包/类
static public InputStream tryOpenAsCompressedFile (File file, String mimeType, String contentEncoding) {
String fileName = file.getName ();
try {
if (fileName.endsWith (".gz")
|| "gzip".equals (contentEncoding)
|| "x-gzip".equals (contentEncoding)
|| "application/x-gzip".equals (mimeType)) {
return new GZIPInputStream (new FileInputStream (file));
} else if (fileName.endsWith (".bz2")
|| "application/x-bzip2".equals (mimeType)) {
InputStream is = new FileInputStream (file);
is.mark (4);
if (!(is.read () == 'B' && is.read () == 'Z')) {
// No BZ prefix as appended by command line tools. Reset and hope for
// the best
is.reset ();
}
return new CBZip2InputStream (is);
}
} catch (IOException e) {
logger.warn ("Something that looked like a compressed file gave an error on open: " + file, e);
}
return null;
}
示例10: getInputSource
import org.apache.tools.bzip2.CBZip2InputStream; //导入依赖的package包/类
/**
*
* @return An InputSource created from wikiXMLFile
* @throws Exception
*/
protected InputSource getInputSource() throws Exception
{
BufferedReader br = null;
if(this.wikiXMLBufferedReader != null) {
br = this.wikiXMLBufferedReader;
} else if(wikiXMLFile.endsWith(".gz")) {
br = new BufferedReader(new InputStreamReader(
new GZIPInputStream(new FileInputStream(wikiXMLFile)), "UTF8"));
} else if(wikiXMLFile.endsWith(".bz2")) {
FileInputStream fis = new FileInputStream(wikiXMLFile);
byte [] ignoreBytes = new byte[2];
fis.read(ignoreBytes); //"B", "Z" bytes from commandline tools
br = new BufferedReader(new InputStreamReader(
new CBZip2InputStream(fis), "UTF8"));
} else {
br = new BufferedReader(new InputStreamReader(
new FileInputStream(wikiXMLFile), "UTF8"));
}
return new InputSource(br);
}
示例11: read
import org.apache.tools.bzip2.CBZip2InputStream; //导入依赖的package包/类
public InputStream read() {
InputStream is = resource.read();
try {
// CBZip2InputStream expects the opening "BZ" to be skipped
byte[] skip = new byte[2];
is.read(skip);
return new CBZip2InputStream(is);
} catch (Exception e) {
IOUtils.closeQuietly(is);
throw ResourceExceptions.readFailed(resource.getDisplayName(), e);
}
}
示例12: decompressMultiBlock
import org.apache.tools.bzip2.CBZip2InputStream; //导入依赖的package包/类
/**
* Decompresses a block which was compressed using the multi compression method.
*
* @param block block to be decompressed
* @param outSize size of the decompressed data
* @param dest buffer to copy the decompressed data
* @param destPos position in the destination buffer to copy the decompressed data
* @throws InvalidMpqArchiveException if unsupported compression is found or the decompression of the block fails
*/
public static void decompressMultiBlock( byte[] block, final int outSize, final byte[] dest, final int destPos ) throws InvalidMpqArchiveException {
// Check if block is really compressed, some blocks have set the compression flag, but are not compressed.
if ( block.length >= outSize ) {
// Copy block
System.arraycopy( block, 0, dest, destPos, outSize );
} else {
final byte compressionFlag = block[ 0 ];
switch ( compressionFlag ) {
case FLAG_COMPRESSION_ZLIB : {
// Handle deflated code (compressionFlag = 0x02)
final Inflater inflater = new Inflater();
inflater.setInput( block, 1, block.length - 1 );
try {
inflater.inflate( dest, destPos, outSize );
inflater.end();
} catch ( final DataFormatException dfe ) {
throw new InvalidMpqArchiveException( "Data format exception, failed to decompressed block!", dfe );
}
break;
// End of inflating
}
case FLAG_COMPRESSION_BZIP2 : {
try ( final CBZip2InputStream cis = new CBZip2InputStream( new ByteArrayInputStream( block, 3, block.length - 3 ) ) ) {
cis.read( dest );
} catch ( final IOException ie ) {
throw new InvalidMpqArchiveException( "Data format exception, failed to decompressed block!", ie );
}
break;
}
default :
throw new InvalidMpqArchiveException( "Compression (" + compressionFlag + ") not supported!" );
}
}
}
示例13: createInputStream
import org.apache.tools.bzip2.CBZip2InputStream; //导入依赖的package包/类
/**
* Creates an input stream to read from a regular or compressed file.
*
* @param fileName a file name with an extension (.gz or .bz2) or without it;
* if the user specifies an extension .gz or .bz2,
* we assume that the input
* file is compressed.
* @return an input stream to read from the file.
* @throws IOException
*/
public static InputStream createInputStream(String fileName) throws IOException {
InputStream finp = new FileInputStream(fileName);
if (fileName.endsWith(".gz")) return new GZIPInputStream(finp);
if (fileName.endsWith(".bz2")) {
finp.read(new byte[2]); // skip the mark
return new CBZip2InputStream(finp);
}
return finp;
}
示例14: openBZip2Stream
import org.apache.tools.bzip2.CBZip2InputStream; //导入依赖的package包/类
static InputStream openBZip2Stream(InputStream infile) throws IOException {
int first = infile.read();
int second = infile.read();
if (first != 'B' || second != 'Z') {
throw new IOException("Didn't find BZ file signature in .bz2 file");
}
return new CBZip2InputStream(infile);
}
示例15: decompress
import org.apache.tools.bzip2.CBZip2InputStream; //导入依赖的package包/类
/**
* Uncompress bz2 file
*
* @param path
* path to file to uncompress
* @throws IOException
*/
public void decompress(String path)
throws IOException
{
File bzip2 = new File(path);
//
File unarchived = new File(bzip2.getName().replace(".bz2", ""));
unarchived.createNewFile();
BufferedInputStream inputStr = new BufferedInputStream(new FileInputStream(bzip2));
// read bzip2 prefix
inputStr.read();
inputStr.read();
BufferedInputStream buffStr = new BufferedInputStream(inputStr);
CBZip2InputStream input = new CBZip2InputStream(buffStr);
FileOutputStream outStr = new FileOutputStream(unarchived);
while (true) {
byte[] compressedBytes = new byte[DECOMPRESSION_CACHE];
int byteRead = input.read(compressedBytes);
outStr.write(compressedBytes, 0, byteRead);
if (byteRead != DECOMPRESSION_CACHE) {
break;
}
}
input.close();
buffStr.close();
inputStr.close();
outStr.close();
}