dsfds2343 2012-08-26 09:17
浏览 98
已采纳

PHP中的PHP gzcompress

I try to compress string in Java like php's gzcompress function does it. But I can't get the same byte arrays in result.

My php code:

<?php
echo 'This is a test';
$compressed = gzcompress('This is a test');
echo '<br>';
for ($i=0; $i<strlen($compressed); $i++) {
    echo '['.$i.'] = '.ord($compressed[$i]).'<br>';
}
?>

The result:

This is a test

[0] = 120 
[1] = 156 
[2] = 11 
[3] = 201 
[4] = 200 
[5] = 44 
[6] = 86 
[7] = 0 
[8] = 162 
[9] = 68 
[10] = 133 
[11] = 146 
[12] = 212 
[13] = 226 
[14] = 18 
[15] = 0 
[16] = 36 
[17] = 115 
[18] = 4 
[19] = 246 

Java code:

public class Main {

    public static byte[] compressString(String uncompressedString) throws IllegalArgumentException, IllegalStateException {
        try {
            byte[] utfEncodedBytes = uncompressedString.getBytes();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(baos)) {
                gzipOutputStream.write(utfEncodedBytes);
                gzipOutputStream.finish();
            }
            return baos.toByteArray();
        } catch (Exception e) {
            throw new IllegalStateException("GZIP compression failed: " + e, e);
        }
    }

    public static void main(String[] args) {
        String input = "This is a test";
        System.out.println("Input:  " + input);
        byte[] compressed = compressString(input);
        for (int i = 0; i < compressed.length; i++) {
            System.out.println("[" + i + "] = " + (compressed[i] & 0xFF));
        }
    }
}

The result:

Input: This is a test

[0] = 31 
[1] = 139 
[2] = 8 
[3] = 0 
[4] = 0 
[5] = 0 
[6] = 0 
[7] = 0 
[8] = 0 
[9] = 0 
[10] = 11 
[11] = 201 
[12] = 200 
[13] = 44 
[14] = 86 
[15] = 0 
[16] = 162 
[17] = 68 
[18] = 133 
[19] = 146 
[20] = 212 
[21] = 226 
[22] = 18 
[23] = 0 
[24] = 50 
[25] = 159 
[26] = 122 
[27] = 192 
[28] = 14
[29] = 0 
[30] = 0 
[31] = 0 

Middle parts of compressed arrays are the same. But they are different in the start and in the end.

  • 写回答

2条回答 默认 最新

  • doucaishi0077 2012-08-26 10:19
    关注

    The documentation for the gzcompress function specifically states that the compression algorithm it uses is zlib, which is

    not the same as gzip compression, which includes some header data. See gzencode() for gzip compression.

    Java's GZIPOutputStream does proper gzip compression including the right headers. DeflaterOutputStream does plain deflate without the gzip headers, which may be closer to what you are after, or there are third party solutions such as JZlib.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么