本文整理匯總了Java中java.util.zip.Deflater類的典型用法代碼示例。如果您正苦於以下問題:Java Deflater類的具體用法?Java Deflater怎麽用?Java Deflater使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Deflater類屬於java.util.zip包,在下文中一共展示了Deflater類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: zipCompression
import java.util.zip.Deflater; //導入依賴的package包/類
private String zipCompression(String data) throws UnsupportedEncodingException, IOException {
Deflater zipDeflater = new Deflater();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
zipDeflater.setInput(getBytes(data));
zipDeflater.finish();
byte[] buffer = new byte[1024];
int count = 0;
while (!zipDeflater.finished()) {
count = zipDeflater.deflate(buffer);
stream.write(buffer, 0, count);
}
return new String(Base64.getEncoder().encode(stream.toByteArray()), LOCAL_ENCODING);
} finally {
stream.close();
zipDeflater.end();
}
}
示例2: compressForZlib
import java.util.zip.Deflater; //導入依賴的package包/類
/**
* zlib compress 2 byte
*
* @param bytesToCompress
* @return
*/
public static byte[] compressForZlib(byte[] bytesToCompress) {
Deflater deflater = new Deflater();
deflater.setInput(bytesToCompress);
deflater.finish();
byte[] bytesCompressed = new byte[Short.MAX_VALUE];
int numberOfBytesAfterCompression = deflater.deflate(bytesCompressed);
byte[] returnValues = new byte[numberOfBytesAfterCompression];
System.arraycopy
(
bytesCompressed,
0,
returnValues,
0,
numberOfBytesAfterCompression
);
return returnValues;
}
示例3: deflater
import java.util.zip.Deflater; //導入依賴的package包/類
/**
* 壓縮.
*
* @param inputByte
* 需要解壓縮的byte[]數組
* @return 壓縮後的數據
* @throws IOException
*/
public static byte[] deflater(final byte[] inputByte) throws IOException {
int compressedDataLength = 0;
Deflater compresser = new Deflater();
compresser.setInput(inputByte);
compresser.finish();
ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length);
byte[] result = new byte[1024];
try {
while (!compresser.finished()) {
compressedDataLength = compresser.deflate(result);
o.write(result, 0, compressedDataLength);
}
} finally {
o.close();
}
compresser.end();
return o.toByteArray();
}
示例4: compress
import java.util.zip.Deflater; //導入依賴的package包/類
public static byte[] compress(final byte[] data) {
final Deflater deflater = new Deflater();
deflater.setInput(data);
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
deflater.finish();
final byte[] buffer = new byte[1024];
try {
while (!deflater.finished()) {
final int count = deflater.deflate(buffer); // returns the generated
// code...
// index
outputStream.write(buffer, 0, count);
}
outputStream.close();
} catch (final IOException e) {
log.log(Level.SEVERE, e.getMessage(), e);
}
return outputStream.toByteArray();
}
示例5: openFile
import java.util.zip.Deflater; //導入依賴的package包/類
protected void openFile() throws HsqlException {
try {
FileAccess fa = database.getFileAccess();
java.io.OutputStream fos = fa.openOutputStreamElement(outFile);
outDescriptor = fa.getFileSync(fos);
fileStreamOut = new DeflaterOutputStream(fos,
new Deflater(Deflater.DEFAULT_COMPRESSION), bufferSize);
} catch (IOException e) {
throw Trace.error(Trace.FILE_IO_ERROR, Trace.Message_Pair,
new Object[] {
e.toString(), outFile
});
}
}
示例6: deflater
import java.util.zip.Deflater; //導入依賴的package包/類
/**
* 壓縮.
*
* @param inputByte 需要解壓縮的byte[]數組
* @return 壓縮後的數據
* @throws IOException
*/
public static byte[] deflater(final byte[] inputByte) throws IOException {
int compressedDataLength = 0;
Deflater compresser = new Deflater();
compresser.setInput(inputByte);
compresser.finish();
ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length);
byte[] result = new byte[1024];
try {
while (!compresser.finished()) {
compressedDataLength = compresser.deflate(result);
o.write(result, 0, compressedDataLength);
}
} finally {
o.close();
}
compresser.end();
return o.toByteArray();
}
示例7: deflate
import java.util.zip.Deflater; //導入依賴的package包/類
public static byte[] deflate(byte[] data, int level) throws Exception {
Deflater deflater = new Deflater(level);
deflater.reset();
deflater.setInput(data);
deflater.finish();
ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
byte[] buf = new byte[1024];
try {
while (!deflater.finished()) {
int i = deflater.deflate(buf);
bos.write(buf, 0, i);
}
} finally {
deflater.end();
}
return bos.toByteArray();
}
示例8: openFile
import java.util.zip.Deflater; //導入依賴的package包/類
protected void openFile() {
try {
FileAccess fa = database.getFileAccess();
java.io.OutputStream fos = fa.openOutputStreamElement(outFile);
outDescriptor = fa.getFileSync(fos);
fileStreamOut = new DeflaterOutputStream(fos,
new Deflater(Deflater.DEFAULT_COMPRESSION), bufferSize);
} catch (IOException e) {
throw Error.error(ErrorCode.FILE_IO_ERROR,
ErrorCode.M_Message_Pair, new Object[] {
e.toString(), outFile
});
}
}
示例9: Packer
import java.util.zip.Deflater; //導入依賴的package包/類
public Packer(final File destFile,
final Signer signer,
final Boolean inPlace) throws FileNotFoundException {
this.inPlace = inPlace;
if (inPlace) { //In codesign.py this is what we use
this.zipStream = new ZipOutputStream(
new DataOutputStream(
new ByteArrayOutputStream(128*1024*1024-1))); //Avoid java bug https://bugs.openjdk.java.net/browse/JDK-8055949 by being able to get to max buffer size of MAX_INT-16
zipStream.setLevel(Deflater.NO_COMPRESSION);
} else {
this.zipStream = new ZipOutputStream(
new BufferedOutputStream(
new FileOutputStream(destFile)));
}
this.signer = signer;
}
示例10: dumpFlate
import java.util.zip.Deflater; //導入依賴的package包/類
/** Write the entire content into the given file using Flate compression (see RFC1951) then return the number of bytes written. */
public long dumpFlate(RandomAccessFile os) throws IOException {
Deflater zip = new Deflater(Deflater.BEST_COMPRESSION);
byte[] output = new byte[8192];
Iterator<byte[]> it = list.iterator(); // when null, that means we have told the Deflater that no more input would be coming
long ans = 0; // the number of bytes written out so far
while(true) {
if (it!=null && zip.needsInput() && it.hasNext()) {
byte[] in = it.next();
if (in == list.getLast()) { zip.setInput(in, 0, n); it=null; zip.finish(); } else { zip.setInput(in, 0, SIZE); }
}
if (it==null && zip.finished()) break;
int count = zip.deflate(output);
if (count > 0) {
ans = ans + count;
if (ans < 0) throw new IOException("Data too large to be written to the output file.");
os.write(output, 0, count);
}
}
return ans;
}
示例11: createEncoder
import java.util.zip.Deflater; //導入依賴的package包/類
@Override
public OneWayCodec createEncoder() throws Exception {
return new OneWayCodec() {
private final Deflater deflater = new Deflater(compressionLevel);
@Override
public byte[] code(final byte[] data) throws Exception {
deflater.reset();
final ByteArrayOutputStream bout = new ByteArrayOutputStream(data.length / 2);
try (final DeflaterOutputStream out = new DeflaterOutputStream(bout, deflater)) {
out.write(data);
}
return bout.toByteArray();
}
};
}
示例12: compress
import java.util.zip.Deflater; //導入依賴的package包/類
public static byte[] compress(byte[] value, int offset, int length, int compressionLevel) {
ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
Deflater compressor = new Deflater();
try {
compressor.setLevel(compressionLevel); // 將當前壓縮級別設置為指定值。
compressor.setInput(value, offset, length);
compressor.finish(); // 調用時,指示壓縮應當以輸入緩衝區的當前內容結尾。
// Compress the data
final byte[] buf = new byte[1024];
while (!compressor.finished()) {
// 如果已到達壓縮數據輸出流的結尾,則返回 true。
int count = compressor.deflate(buf);
// 使用壓縮數據填充指定緩衝區。
bos.write(buf, 0, count);
}
} finally {
compressor.end(); // 關閉解壓縮器並放棄所有未處理的輸入。
}
return bos.toByteArray();
}
示例13: deflate
import java.util.zip.Deflater; //導入依賴的package包/類
public static byte[] deflate(byte[] data, int level) throws Exception {
Deflater deflater = new Deflater(level);
deflater.reset();
deflater.setInput(data);
deflater.finish();
ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
byte[] buf = new byte[1024];
try {
while (!deflater.finished()) {
int i = deflater.deflate(buf);
bos.write(buf, 0, i);
}
} finally {
deflater.end();
bos.close();
}
return bos.toByteArray();
}
示例14: flush
import java.util.zip.Deflater; //導入依賴的package包/類
@Override
public synchronized void flush() throws IOException {
if (hasLastByte) {
// - do not allow the gzip header to be flushed on its own
// - do not do anything if there is no data to send
// trick the deflater to flush
/**
* Now this is tricky: We force the Deflater to flush its data by
* switching compression level. As yet, a perplexingly simple workaround
* for
* http://developer.java.sun.com/developer/bugParade/bugs/4255743.html
*/
if (!def.finished()) {
def.setLevel(Deflater.NO_COMPRESSION);
flushLastByte();
flagReenableCompression = true;
}
}
out.flush();
}
示例15: compress
import java.util.zip.Deflater; //導入依賴的package包/類
public static byte[] compress(byte input[]) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Deflater compressor = new Deflater(1);
try {
compressor.setInput(input);
compressor.finish();
final byte[] buf = new byte[2048];
while (!compressor.finished()) {
int count = compressor.deflate(buf);
bos.write(buf, 0, count);
}
} finally {
compressor.end();
}
return bos.toByteArray();
}