• zip包解压时报malformed input off : 0, length : 1


    1.错误发现

    项目中有压缩导出导入,自身的导出压缩再导入使用ZipInputStream 没有问题,但是有需求对压缩包解压人工干预修改后压缩导入导致解压报错:malformed input off : 0, length : 1

    2.查找资料替换

    网上查找发现ZipInputStream 解压缩对压缩包内部,若内部节点(文件或目录)名称包含非拉丁文,跨平台传递时,比如压缩方用的utf-8,接收方用的是gbk,Jdk原生库解压流解析就会报错。可以使用ZipArchiveInputStream。

    使用ZipArchiveInputStream而非ZipInputStream的原因主要有以下两点:

    1. 支持更多的压缩格式:ZipArchiveInputStream是Apache Commons Compress库中提供的类,能够支持多种压缩格式,包括Zip、Gzip、Tar、Jar等。而ZipInputStream是Java标准库中的类,只能读取普通的Zip文件。因此,如果需要处理多种压缩格式的文件,使用ZipArchiveInputStream会更加方便。
    2. 更多的选项和功能:ZipArchiveInputStream提供了更多的选项和功能,例如可以设置编码方式、支持密码保护的Zip文件等。这些额外的功能使得ZipArchiveInputStream在处理压缩文件时更加灵活和强大。

    综上所述,如果需要处理多种压缩格式的文件或者需要更多的选项和功能,建议使用ZipArchiveInputStream。

    3.ZipArchiveInputStream使用

    引入pom依赖:

    1. <dependency>
    2. <groupId>org.apache.commons</groupId>
    3. <artifactId>commons-compress</artifactId>
    4. <version>1.18</version>
    5. </dependency>

    代码实现:

    1. /**
    2. * 流zip工具类
    3. */
    4. public final classZipUtil {
    5. /**
    6. * 解压流
    7. *
    8. * @param inputStream
    9. * @return
    10. */
    11. @SneakyThrows
    12. public static InputStream unzipStream(InputStream inputStream) {
    13. if (inputStream == null) {
    14. return null;
    15. }
    16. //1.Jdk原生Zip流,会因为文件或文件夹命名所用 字符集编码不匹配,报MALFORMED错(畸形的)
    17. //ZipInputStream zipInputStream = new ZipInputStream(inputStream);
    18. //2.Apach-commons-compress的Zip流,兼容性更好
    19. ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(inputStream);
    20. ByteArrayOutputStream bos = new ByteArrayOutputStream();
    21. while (zipInputStream.getNextEntry() != null) {
    22. int n;
    23. byte[] buff = new byte[1024];
    24. while ((n = zipInputStream.read(buff)) != -1) {
    25. bos.write(buff, 0, n);
    26. }
    27. }
    28. bos.flush();
    29. bos.close();
    30. return new ByteArrayInputStream(bos.toByteArray());
    31. }
    32. /**
    33. * 压缩流
    34. *
    35. * @param txtName
    36. * @param inputStream
    37. * @return
    38. */
    39. public static ByteArrayOutputStream zipStream(String txtName, ByteArrayInputStream inputStream) {
    40. //Fail-Fast
    41. if (inputStream == null) {
    42. return null;
    43. }
    44. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    45. ZipOutputStream zipOut = new ZipOutputStream(outputStream);
    46. zipOut.putNextEntry(new ZipEntry(txtName));
    47. int n;
    48. byte[] buffer = new byte[1024];
    49. while ((n = inputStream.read(buffer)) != -1) {
    50. zipOut.write(buffer, 0, n);
    51. }
    52. zipOut.close();
    53. inputStream.close();
    54. outputStream.close();
    55. return outputStream;
    56. }
    57. }

  • 相关阅读:
    codeforces:F. Kirei and the Linear Function【暴力优化 + 避开大数运算 + 避开str切片转int】
    沙猫群优化算法(Sand Cat Swarm Optimization,SCSO) -- 笔记
    Linux11-权限的介绍 rwx详解 修改权限 修改文件目录所有者 修改文件目录所在组 一个实践和两个练习
    [python 刷题] 981 Time Based Key-Value Store
    【docker】数据卷和数据卷容器
    煤矿生产高精专!选矿厂 3D 可视化监管,实现提质增效
    天津市专业大数据培训班,大数据就业岗位的多样性
    基于PHP+MySQL青年志愿者服务管理系统的设计与实现
    [ACTF2020 新生赛] Include1
    lua基础之table
  • 原文地址:https://blog.csdn.net/yztezhl/article/details/133919840