我需要使用ZipOutputStream压缩字节数组,然后检索此数组。之后,原始数组应对应于新数组。 为什么在此代码中2个字节的数组具有相同的值(返回字符串时),但它们不相等:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class Main {
public static void main( String[] args ) throws IOException {
byte[] b = "Help me please".getBytes();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try( ZipOutputStream zos = new ZipOutputStream( baos ) ) {
ZipEntry out = new ZipEntry( "1 First" );
zos.putNextEntry( out );
zos.write( b, 0, b.length );
zos.closeEntry();
zos.close();
}
byte[] a = baos.toByteArray(); //compressed array
ByteArrayInputStream bais = new ByteArrayInputStream( a );
try( ZipInputStream zis = new ZipInputStream(bais)) {
while(zis.getNextEntry() != null ){
byte[] c = new byte[a.length];
int length = zis.read( c, 0, c.length );
System.out.println( "Data: " + new String( c, 0, length));
System.out.println(c.equals(b));
zis.closeEntry();
}
}
}
}
输出:
Data: Help me please
false