考虑到我只能使用JDK 7,我如何将这种方法从C#解压缩从C#转换为Java ?? https://github.com/AresChat/cb0t/blob/master/cb0t/Misc/Zip.cs
也许我应该在另一个尝试中写一个尝试。但这不是最大的问题。除其他外,我可能必须找到等效的MemoryStream,Stream和InflaterInputStream。
有人可以帮助我将此方法转换为Java吗?
没有确切的等效项,因为API的设计不同。特别是Stream没有等效项:相反,您具有单独的InputStream和OutputStream类。
c#MemoryStream在Java中是ByteArrayInputStream或-OutputStream,这取决于您使用它来读取还是写入。
[the API specification中有一个有关如何使用Deflater和Inflater类的示例:
try {
// Encode a String into bytes
String inputString = "blahblahblah";
byte[] input = inputString.getBytes("UTF-8");
// Compress the bytes
byte[] output = new byte[100];
Deflater compresser = new Deflater();
compresser.setInput(input);
compresser.finish();
int compressedDataLength = compresser.deflate(output);
compresser.end();
// Decompress the bytes
Inflater decompresser = new Inflater();
decompresser.setInput(output, 0, compressedDataLength);
byte[] result = new byte[100];
int resultLength = decompresser.inflate(result);
decompresser.end();
// Decode the bytes into a String
String outputString = new String(result, 0, resultLength, "UTF-8");
} catch(java.io.UnsupportedEncodingException ex) {
// handle
} catch (java.util.zip.DataFormatException ex) {
// handle
}
您可以将代码从C#到Java几乎一一翻译。
Java的Stream API与C#有点不同。 Java在读取和写入之间区分流:InputStream用于读取,OutputStream用于写入。
代替Java中使用C#的MemoryStream
和ByteArrayInputStream。
JRE 7包含类ByteArrayOutputStream和DeflaterOutputStream,它们与您提供的C#实现中使用的大多数相同。
C#代码的示例翻译:
InflaterInputStream
为了减少两个功能中的代码,使用了import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
public class Compressor {
// buffer size: usually 1024 (=1KB), but 65536 (=64KB) is faster in the most cases
// for larger data, but wasting memory for smaller data
private static final int BUFFER_SIZE = 65536;
// compresses a byte[] with Java's default Deflate compression.
public byte[] compress(byte[] data) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (InputStream byteArrayInputStream = new ByteArrayInputStream(data);
DeflaterOutputStream deflaterInputStream = new DeflaterOutputStream(byteArrayOutputStream)
) {
// this copies from the streams
int read;
byte[] buffer = new byte[BUFFER_SIZE];
while ((read = byteArrayInputStream.read(buffer)) != -1) {
deflaterInputStream.write(buffer, 0, read);
}
}
return byteArrayOutputStream.toByteArray();
}
// decompress a byte[]
public byte[] decompress(byte[] data) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (InputStream byteArrayInputStream = new ByteArrayInputStream(data);
InflaterInputStream inflaterInputStream = new InflaterInputStream(byteArrayInputStream);
) {
// this copies from the streams
int read;
byte[] buffer = new byte[BUFFER_SIZE];
while ((read = inflaterInputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer,0, read);
}
}
return byteArrayOutputStream.toByteArray();
}
}
。该函数抛出try-with-resources statement,用于此类之外的异常处理。
请注意,在返回结果之前,请先关闭IOExceptions,否则会破坏结果DeflaterOutputStream
。
您可以通过重构从流中复制的部分或使用Apache Commons IO byte[]
函数来优化此代码。
您还可以在IOUtlis::copy的构造函数Deflater上使用自定义配置的Deflater(如压缩级别更高,请参见DeflaterOutputStream。