Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java.util.zip压缩字节数组?_Java_Zip - Fatal编程技术网

如何使用java.util.zip压缩字节数组?

如何使用java.util.zip压缩字节数组?,java,zip,Java,Zip,我有这段代码来压缩浮点值,但是输出的大小比原始值大 Mi的目标是将压缩数据(字节数组)保存在一个文件中,然后对数据进行膨胀以获得原始浮点值 我做错了什么 public void floatToArrayByte() throws IOException { float f = 3574.34568f; byte arayByte[] = ByteBuffer.allocate(4).putFloat(f).array(); System.out.println("Or

我有这段代码来压缩浮点值,但是输出的大小比原始值大

Mi的目标是将压缩数据(字节数组)保存在一个文件中,然后对数据进行膨胀以获得原始浮点值

我做错了什么

public void floatToArrayByte() throws IOException {
    float f = 3574.34568f;

    byte arayByte[] = ByteBuffer.allocate(4).putFloat(f).array();

    System.out.println("Original values");

    for (int i = 0; i < arayByte.length; i++) {
        System.out.print(arayByte[i]);
        System.out.print(" ");
    }

    System.out.println("");  

    arayByte = this.compress(arayByte);

    System.out.println("Compress values");

    for (int i = 0; i < arayByte.length; i++) {
        System.out.print(arayByte[i]);
        System.out.print(" ");
    }

    System.out.println("");
}

public byte[] compress(byte[] data) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setInput(data);
    deflater.setLevel(Deflater.DEFLATED);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    deflater.finish();
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();
    System.out.println("Original size: " + data.length + " Bytes");
    System.out.println("Compressed size: " + output.length + " Bytes");
    return output;
}
public void floatToArrayByte()引发IOException{
浮球f=3574.34568f;
字节arayByte[]=ByteBuffer.allocate(4.putFloat(f.array();
System.out.println(“原始值”);
for(int i=0;i
这就是我得到的

原值

69 95 101-120

原始大小:4字节

压缩大小:12字节

压缩值

120-38 115-115 79-19 0 0 3-121 1-110

对于非常短的输入(在您的示例中只有4个字节),您将始终获得比输入长的“压缩”值,因为压缩流包含一些头


试着压缩更多的数据(比如10万个浮点数)以查看差异。

这是您压缩四项的意图吗?很可能你不会节省太多空间。即使有400个浮点数,结果也可能会很糟糕,特别是如果浮点数是随机的。我要压缩一个包含数千条记录的气候数据集,所有的值都是浮点数。你能帮我吗?