public class ZlibStream
extends com.aspose.ms.System.IO.Stream
implements com.aspose.ms.System.IDisposable
Represents a Zlib stream for compression or decompression.
The ZlibStream is a Decorator
on a System.IO.Stream
. It adds ZLIB compression or decompression to any
stream.
Using this stream, applications can compress or decompress data via
stream Read
and Write
operations. Either compresssion or
decompression can occur through either reading or writing. The compression
format used is ZLIB, which is documented in IETF RFC 1950
, "ZLIB Compressed
Data Format Specification version 3.3". This implementation of ZLIB always uses
DEFLATE as the compression method. (see IETF RFC 1951
, "DEFLATE
Compressed Data Format Specification version 1.3.")
The ZLIB format allows for varying compression methods, window sizes, and dictionaries. This implementation always uses the DEFLATE compression method, a preset dictionary, and 15 window bits by default.
This class is similar to DeflateStream
, except that it adds the
RFC1950 header and trailer bytes to a compressed stream when compressing, or expects
the RFC1950 header and trailer bytes when decompressing. It is also similar to the
GZipStream
.
DeflateStream
,
GZipStream
Constructor and Description |
---|
ZlibStream(com.aspose.ms.System.IO.Stream stream,
int mode)
Create a ZlibStream using the specified CompressionMode.
|
ZlibStream(com.aspose.ms.System.IO.Stream stream,
int mode,
boolean leaveOpen)
Create a ZlibStream using the specified CompressionMode, and explicitly specify
whether the captive stream should be left open after Deflation or Inflation.
|
ZlibStream(com.aspose.ms.System.IO.Stream stream,
int mode,
int level)
Create a ZlibStream using the specified CompressionMode and the specified CompressionLevel.
|
ZlibStream(com.aspose.ms.System.IO.Stream stream,
int mode,
int level,
boolean leaveOpen)
Create a ZlibStream using the specified CompressionMode and the specified
CompressionLevel, and explicitly specify whether the stream should be left open
after Deflation or Inflation.
|
Modifier and Type | Method and Description |
---|---|
boolean |
canRead()
Indicates whether the stream can be read.
|
boolean |
canSeek()
Indicates whether the stream supports Seek operations.
|
boolean |
canWrite()
Indicates whether the stream can be written.
|
void |
close() |
static byte[] |
compressBuffer(byte[] b)
Compress a byte array into a new byte array.
|
static byte[] |
compressString(String s)
Compress a string into a byte array.
|
void |
dispose() |
void |
flush()
Flush the stream.
|
int |
getBufferSize()
The size of the working buffer for the compression codec.
|
int |
getFlushMode()
This property sets the flush behavior on the stream.
|
long |
getLength()
Reading this property always throws a NotImplementedException.
|
long |
getPosition()
The position of the stream pointer.
|
long |
getTotalIn()
Returns the total number of bytes input so far.
|
long |
getTotalOut()
Returns the total number of bytes output so far.
|
int |
read(byte[] buffer,
int offset,
int count)
Read data from the stream.
|
long |
seek(long offset,
int origin)
Calling this method always throws a NotImplementedException.
|
void |
setBufferSize(int value) |
void |
setFlushMode(int value) |
void |
setLength(long value)
Calling this method always throws a NotImplementedException.
|
void |
setPosition(long value) |
static byte[] |
uncompressBuffer(byte[] compressed)
Uncompress a byte array into a byte array.
|
static String |
uncompressString(byte[] compressed)
Uncompress a byte array into a single string.
|
void |
write(byte[] buffer,
int offset,
int count)
Write data to the stream.
|
public ZlibStream(com.aspose.ms.System.IO.Stream stream, int mode)
Create a ZlibStream using the specified CompressionMode.
This example uses a ZlibStream to compress a file, and writes the compressed data to another file.using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress)) { using (var raw = System.IO.File.Create(fileToCompress + ".zlib")) { using (Stream compressor = new ZlibStream(raw, CompressionMode.Compress)) { byte[] buffer = new byte[WORKING_BUFFER_SIZE]; int n; while ((n= input.Read(buffer, 0, buffer.Length)) != 0) { compressor.Write(buffer, 0, n); } } } }Using input As Stream = File.OpenRead(fileToCompress) Using raw As FileStream = File.Create(fileToCompress & ".zlib") Using compressor As Stream = New ZlibStream(raw, CompressionMode.Compress) Dim buffer As Byte() = New Byte(4096) {} Dim n As Integer = -1 Do While (n <> 0) If (n > 0) Then compressor.Write(buffer, 0, n) End If n = input.Read(buffer, 0, buffer.Length) Loop End Using End Using End Using
stream
- The stream which will be read or written.mode
- Indicates whether the ZlibStream will compress or decompress.
When mode is CompressionMode.Compress
, the ZlibStream will use
the default compression level. The "captive" stream will be closed when the
ZlibStream is closed.
public ZlibStream(com.aspose.ms.System.IO.Stream stream, int mode, int level)
Create a ZlibStream using the specified CompressionMode and the specified CompressionLevel.
This example uses a ZlibStream to compress data from a file, and writes the compressed data to another file.using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress)) { using (var raw = System.IO.File.Create(fileToCompress + ".zlib")) { using (Stream compressor = new ZlibStream(raw, CompressionMode.Compress, CompressionLevel.BestCompression)) { byte[] buffer = new byte[WORKING_BUFFER_SIZE]; int n; while ((n= input.Read(buffer, 0, buffer.Length)) != 0) { compressor.Write(buffer, 0, n); } } } }Using input As Stream = File.OpenRead(fileToCompress) Using raw As FileStream = File.Create(fileToCompress & ".zlib") Using compressor As Stream = New ZlibStream(raw, CompressionMode.Compress, CompressionLevel.BestCompression) Dim buffer As Byte() = New Byte(4096) {} Dim n As Integer = -1 Do While (n <> 0) If (n > 0) Then compressor.Write(buffer, 0, n) End If n = input.Read(buffer, 0, buffer.Length) Loop End Using End Using End Using
stream
- The stream to be read or written while deflating or inflating.mode
- Indicates whether the ZlibStream will compress or decompress.level
- A tuning knob to trade speed for effectiveness.
When mode is CompressionMode.Decompress
, the level parameter is ignored.
The "captive" stream will be closed when the ZlibStream is closed.
public ZlibStream(com.aspose.ms.System.IO.Stream stream, int mode, boolean leaveOpen)
Create a ZlibStream using the specified CompressionMode, and explicitly specify whether the captive stream should be left open after Deflation or Inflation.
stream
- The stream which will be read or written. This is called the
"captive" stream in other places in this documentation.mode
- Indicates whether the ZlibStream will compress or decompress.leaveOpen
- true if the application would like the stream to remain
open after inflation/deflation.
When mode is CompressionMode.Compress
, the ZlibStream will use
the default compression level.
This constructor allows the application to request that the captive stream
remain open after the deflation or inflation occurs. By default, after Close()
is called on the stream, the captive stream is also closed. In some cases this
is not desired, for example if the stream is a System.IO.MemoryStream
that will be re-read after compression. Specify
true for the leaveOpen parameter to leave the stream open.
See the other overloads of this constructor for example code.
public ZlibStream(com.aspose.ms.System.IO.Stream stream, int mode, int level, boolean leaveOpen)
Create a ZlibStream using the specified CompressionMode and the specified CompressionLevel, and explicitly specify whether the stream should be left open after Deflation or Inflation.
This example shows how to use a ZlibStream to compress the data from a file, and store the result into another file. The filestream remains open to allow additional data to be written to it.using (var output = System.IO.File.Create(fileToCompress + ".zlib")) { using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress)) { using (Stream compressor = new ZlibStream(output, CompressionMode.Compress, CompressionLevel.BestCompression, true)) { byte[] buffer = new byte[WORKING_BUFFER_SIZE]; int n; while ((n= input.Read(buffer, 0, buffer.Length)) != 0) { compressor.Write(buffer, 0, n); } } } // can write additional data to the output stream here }Using output As FileStream = File.Create(fileToCompress & ".zlib") Using input As Stream = File.OpenRead(fileToCompress) Using compressor As Stream = New ZlibStream(output, CompressionMode.Compress, CompressionLevel.BestCompression, True) Dim buffer As Byte() = New Byte(4096) {} Dim n As Integer = -1 Do While (n <> 0) If (n > 0) Then compressor.Write(buffer, 0, n) End If n = input.Read(buffer, 0, buffer.Length) Loop End Using End Using ' can write additional data to the output stream here. End Using
stream
- The stream which will be read or written.mode
- Indicates whether the ZlibStream will compress or decompress.leaveOpen
- true if the application would like the stream to remain open after inflation/deflation.level
- A tuning knob to trade speed for effectiveness. This parameter is effective only when
mode is CompressionMode.Compress
.
This constructor allows the application to request that the captive stream
remain open after the deflation or inflation occurs. By default, after Close()
is called on the stream, the captive stream is also closed. In some cases this
is not desired, for example if the stream is a System.IO.MemoryStream
that will be re-read after compression. Specify
true for the leaveOpen parameter to leave the stream open.
When mode is CompressionMode.Decompress
, the level parameter is ignored.
public int getFlushMode()
This property sets the flush behavior on the stream. Sorry, though, not sure exactly how to describe all the various settings.
public void setFlushMode(int value)
public int getBufferSize()
The size of the working buffer for the compression codec.
The working buffer is used for all stream operations. The default size is 1024 bytes. The minimum size is 128 bytes. You may get better performance with a larger buffer. Then again, you might not. You would have to test it.
Set this before the first call to Read() or Write() on the stream. If you try to set it afterwards, it will throw.
public void setBufferSize(int value)
public long getTotalIn()
Returns the total number of bytes input so far.
public long getTotalOut()
Returns the total number of bytes output so far.
public void close()
close
in class com.aspose.ms.System.IO.Stream
public boolean canRead()
Indicates whether the stream can be read.
canRead
in class com.aspose.ms.System.IO.Stream
public boolean canSeek()
Indicates whether the stream supports Seek operations.
canSeek
in class com.aspose.ms.System.IO.Stream
public boolean canWrite()
Indicates whether the stream can be written.
canWrite
in class com.aspose.ms.System.IO.Stream
public void flush()
Flush the stream.
flush
in class com.aspose.ms.System.IO.Stream
public long getLength()
Reading this property always throws a NotImplementedException.
getLength
in class com.aspose.ms.System.IO.Stream
public long getPosition()
The position of the stream pointer.
getPosition
in class com.aspose.ms.System.IO.Stream
public void setPosition(long value)
setPosition
in class com.aspose.ms.System.IO.Stream
public int read(byte[] buffer, int offset, int count)
Read data from the stream.
read
in class com.aspose.ms.System.IO.Stream
buffer
- The buffer into which the read data should be placed.offset
- the offset within that data array to put the first byte read.count
- the number of bytes to read.
If you wish to use the ZlibStream to compress data while reading, you can create a ZlibStream with CompressionMode.Compress, providing an uncompressed data stream. Then call Read() on that ZlibStream, and the data read will be compressed. If you wish to use the ZlibStream to decompress data while reading, you can create a ZlibStream with CompressionMode.Decompress, providing a readable compressed data stream. Then call Read() on that ZlibStream, and the data will be decompressed as it is read.
A ZlibStream can be used for Read() or Write(), but not both.
public long seek(long offset, int origin)
Calling this method always throws a NotImplementedException.
seek
in class com.aspose.ms.System.IO.Stream
public void setLength(long value)
Calling this method always throws a NotImplementedException.
setLength
in class com.aspose.ms.System.IO.Stream
public void write(byte[] buffer, int offset, int count)
Write data to the stream.
write
in class com.aspose.ms.System.IO.Stream
buffer
- The buffer holding data to write to the stream.offset
- the offset within that data array to find the first byte to write.count
- the number of bytes to write.
If you wish to use the ZlibStream to compress data while writing, you can create a ZlibStream with CompressionMode.Compress, and a writable output stream. Then call Write() on that ZlibStream, providing uncompressed data as input. The data sent to the output stream will be the compressed form of the data written. If you wish to use the ZlibStream to decompress data while writing, you can create a ZlibStream with CompressionMode.Decompress, and a writable output stream. Then call Write() on that stream, providing previously compressed data. The data sent to the output stream will be the decompressed form of the data written.
A ZlibStream can be used for Read() or Write(), but not both.
public static byte[] compressString(String s)
Compress a string into a byte array.
s
- A string to compress. The string will first be encoded
using UTF8, then compressed.
ZlibStream.UncompressString(byte[])
.
ZlibStream.UncompressString(byte[])
public static byte[] compressBuffer(byte[] b)
Compress a byte array into a new byte array.
b
- A buffer to compress.
ZlibStream.UncompressBuffer(byte[])
.
ZlibStream.CompressString(string)
,
ZlibStream.UncompressBuffer(byte[])
public static String uncompressString(byte[] compressed)
Uncompress a byte array into a single string.
compressed
- A buffer containing ZLIB-compressed data.ZlibStream.CompressString(String)
public static byte[] uncompressBuffer(byte[] compressed)
Uncompress a byte array into a byte array.
compressed
- A buffer containing ZLIB-compressed data.ZlibStream.CompressBuffer(byte[])
,
ZlibStream.UncompressString(byte[])
public void dispose()
dispose
in interface com.aspose.ms.System.IDisposable
dispose
in class com.aspose.ms.System.IO.Stream
Copyright © 2016 Aspose. All Rights Reserved.