java - ByteArrayOutputStream.toByteArray() 返回随机字节?

标签 java zip arrays java-io

我正在尝试为我重构的方法编写 Junit 测试,以确保该方法仍按预期运行。我注意到一些我无法弄清楚的奇怪行为。

为什么 Java ByteArrayOutputStream.toByteArray() 返回一些随机字节?我认为这可能与内存有关。数组中的 11 字节总是随机。有什么见解吗?

这是我重构的方法。

package foo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

    public static byte[] createZip(Map<String, byte[]> files) throws IOException {
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        final ZipOutputStream zipfile = new ZipOutputStream(bos);
        String fileName = null;
        ZipEntry zipentry = null;
        for (Map.Entry<String, byte[]> entry : files.entrySet()) {
            fileName = entry.getKey();
            zipentry = new ZipEntry(fileName);
            zipfile.putNextEntry(zipentry);
            zipfile.write(files.get(fileName));
        }

        zipfile.close();  
        return bos.toByteArray();  
    }  
}

测试类

package foo;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({})
public class ZipUtilTest {

    @Test
    public void test_createZip() throws Exception {

        // Setup
        Map<String, byte[]> files = new HashMap<String, byte[]>();

        byte[] byteArr = new byte[] { 49, 17, 23,
                -29, -126, 111, -72, -112, 48, 32, 91, -28, -14, 112 };

        files.put("foo.txt", byteArr);

        // Test
        byte[] result = ZipUtil.createZip(files);

        // Validations
        byte[] expectedByteArray1 = new byte[] { 80, 75, 3, 4, 20, 0, 8, 8, 8, 0, 46, -120, -11, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,
                0, 0, 0, 102, 111, 111, 46, 116, 120, 116, 51, 20, 20, 127, -36, -108, -65, 99, -126, -127, 66, -12, -109, 79, 5, 0, 80,
                75, 7, 8, -99, 100, -122, -62, 16, 0, 0, 0, 14, 0, 0, 0, 80, 75, 1, 2, 20, 0, 20, 0, 8, 8, 8, 0, 46, -120, -11, 72, -99,
                100, -122, -62, 16, 0, 0, 0, 14, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 111, 111, 46, 116,
                120, 116, 80, 75, 5, 6, 0, 0, 0, 0, 1, 0, 1, 0, 53, 0, 0, 0, 69, 0, 0, 0, 0, 0 };

        Assert.assertNotNull(result);
        Assert.assertEquals(144, result.length);

        // a few bytes are "random". So test the first 9 never random bytes
        for (int i = 0; i < 10; i++) {
            Assert.assertEquals(expectedByteArray1[i], result[i]);
        }

        // This fails
        // Assert.assertEquals(expectedByteArray1[10], result[10]);

        for (int i = 11; i < 70; i++) {
            Assert.assertEquals(expectedByteArray1[i], result[i]);
        }
    }
}

最佳答案

您的测试用例假定使用相同输入创建的连续 zip 文件将产生完全相同的输出字节。情况似乎并非如此 - zip 文件规范存储从字节 10(小端)开始的文件修改日期和时间。这就是从该位置开始的字节不同的原因。

https://en.wikipedia.org/wiki/Zip_(file_format)

也就是说,我不认为将 zip 文件中的字节与已知 zip 文件中的字节进行比较是一种非常有效的单元测试。更有效的测试是“往返”zip 文件 - 从新创建的存档中提取压缩文件并与已知输入文件进行比较以确保它们匹配。

关于java - ByteArrayOutputStream.toByteArray() 返回随机字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38514422/

相关文章:

excel - VBA 读取 Zip 文件

C#嵌套 : How to index array of geo-poinst

java - 变量的 volatile 写入是否可以避免乱序写入?

java - Spring框架关系映射

php - 压缩多个数据库 PDF blob 文件

c# - 检查是否已使用数组的所有值

java - 从给定数组创建逆序数组

java - 如何使用serialize Collections.synchronizedList和co

java - 类中介器需要哪些 jars

c# - 使用 ionic.zip 创建 zip 文件