如何使用Java

时间:2016-08-25 14:00:30

标签: java linux zip

我正在开发软件以集成到遗留系统中,我将zip文件发送到FTP服务器,遗留系统会定期扫描将文件转储到文件夹中,或者#34;已完成"或" Maybe_corrupted"。

我的文件被系统地转储到" maybe_corrupted"夹。经过调查,这似乎是由于zip文件的结构。

如果我将文件从FTP下载到Windows计算机上,我将解压缩所有zip文件(zip文件中有zip文件)并将文件重新压缩为旧程序接受文件的完全相同的配置。

具体来说,我的问题是 - 如何参数化java.util.zip,我应该修改哪些参数以使其更接近默认的Windows zip实用程序?

文件比较

根据Tobias Otto的建议,我使用文件比较器(UltraCompare)对2个二进制文件进行了比较:

  • 最初从FTP下载并由旧系统拒绝的文件(左)
  • 已接受的解压缩 - 重新压缩文件(右)

简单地比较字节,很明显文件不一样。请注意,红色字节是差异,灰色是相似之处:

enter image description here

邮编详情

正如David Duponchel所建议的那样,我使用zipdetails来提取每个文件的结构。显然这是一个非常强大的工具但是我不确定如何解释输出,如使用指南中所述,输出引用this document

原始档案:

00000 LOCAL HEADER #1       04034B50
00004 Extract Zip Spec      14 '2.0'
00005 Extract OS            00 'MS-DOS'
00006 General Purpose Flag  0808
      [Bits 1-2]            0 'Normal Compression'
      [Bit  3]              1 'Streamed'
      [Bit 11]              1 'Language Encoding'
00008 Compression Method    0008 'Deflated'
0000A Last Mod Time         491E43F7 'Tue Aug 30 08:31:46 2016'
0000E CRC                   00000000
00012 Compressed Length     00000000
00016 Uncompressed Length   00000000
0001A Filename Length       0018
0001C Extra Length          0000
0001E Filename              'Mean.20160830_073000.zip'
00036 PAYLOAD

05002 STREAMING DATA HEADER 08074B50
05006 CRC                   A21A5BC4
0500A Compressed Length     00004FCC
0500E Uncompressed Length   000054D8

05012 LOCAL HEADER #2       04034B50
05016 Extract Zip Spec      14 '2.0'
05017 Extract OS            00 'MS-DOS'
05018 General Purpose Flag  0808
      [Bits 1-2]            0 'Normal Compression'
      [Bit  3]              1 'Streamed'
      [Bit 11]              1 'Language Encoding'
0501A Compression Method    0008 'Deflated'
0501C Last Mod Time         491E43F7 'Tue Aug 30 08:31:46 2016'
05020 CRC                   00000000
05024 Compressed Length     00000000
05028 Uncompressed Length   00000000
0502C Filename Length       0018
0502E Extra Length          0000
05030 Filename              'Mean.20160830_081500.zip'
05048 PAYLOAD

08FFF STREAMING DATA HEADER 08074B50
09003 CRC                   BAE824D6
09007 Compressed Length     00003FB7
0900B Uncompressed Length   000043A3

0900F LOCAL HEADER #3       04034B50
09013 Extract Zip Spec      14 '2.0'
09014 Extract OS            00 'MS-DOS'
09015 General Purpose Flag  0808
      [Bits 1-2]            0 'Normal Compression'
      [Bit  3]              1 'Streamed'
      [Bit 11]              1 'Language Encoding'
09017 Compression Method    0008 'Deflated'
09019 Last Mod Time         491E43F7 'Tue Aug 30 08:31:46 2016'
0901D CRC                   00000000
09021 Compressed Length     00000000
09025 Uncompressed Length   00000000
09029 Filename Length       0018
0902B Extra Length          0000
0902D Filename              'Mean.20160830_071500.zip'
09045 PAYLOAD

0E05E STREAMING DATA HEADER 08074B50
0E062 CRC                   EDC8AE4E
0E066 Compressed Length     00005019
0E06A Uncompressed Length   000054EA

0E06E LOCAL HEADER #4       04034B50
0E072 Extract Zip Spec      14 '2.0'
0E073 Extract OS            00 'MS-DOS'
0E074 General Purpose Flag  0808
      [Bits 1-2]            0 'Normal Compression'
      [Bit  3]              1 'Streamed'
      [Bit 11]              1 'Language Encoding'
0E076 Compression Method    0008 'Deflated'
0E078 Last Mod Time         491E43F7 'Tue Aug 30 08:31:46 2016'
0E07C CRC                   00000000
0E080 Compressed Length     00000000
0E084 Uncompressed Length   00000000
0E088 Filename Length       0018
0E08A Extra Length          0000
0E08C Filename              'Mean.20160830_080000.zip'
0E0A4 PAYLOAD

15444 STREAMING DATA HEADER 08074B50
15448 CRC                   C37437FB
1544C Compressed Length     000073A0
15450 Uncompressed Length   00008054

15454 LOCAL HEADER #5       04034B50
15458 Extract Zip Spec      14 '2.0'
15459 Extract OS            00 'MS-DOS'
1545A General Purpose Flag  0808
      [Bits 1-2]            0 'Normal Compression'
      [Bit  3]              1 'Streamed'
      [Bit 11]              1 'Language Encoding'
1545C Compression Method    0008 'Deflated'
1545E Last Mod Time         491E43F7 'Tue Aug 30 08:31:46 2016'
15462 CRC                   00000000
15466 Compressed Length     00000000
1546A Uncompressed Length   00000000
1546E Filename Length       0018
15470 Extra Length          0000
15472 Filename              'Mean.20160830_070000.zip'
1548A PAYLOAD

19E5D STREAMING DATA HEADER 08074B50
19E61 CRC                   40E52180
19E65 Compressed Length     000049D3
19E69 Uncompressed Length   00005110

19E6D CENTRAL HEADER #1     02014B50
19E71 Created Zip Spec      14 '2.0'
19E72 Created OS            00 'MS-DOS'
19E73 Extract Zip Spec      14 '2.0'
19E74 Extract OS            00 'MS-DOS'
19E75 General Purpose Flag  0808
      [Bits 1-2]            0 'Normal Compression'
      [Bit  3]              1 'Streamed'
      [Bit 11]              1 'Language Encoding'
19E77 Compression Method    0008 'Deflated'
19E79 Last Mod Time         491E43F7 'Tue Aug 30 08:31:46 2016'
19E7D CRC                   A21A5BC4
19E81 Compressed Length     00004FCC
19E85 Uncompressed Length   000054D8
19E89 Filename Length       0018
19E8B Extra Length          0000
19E8D Comment Length        0000
19E8F Disk Start            0000
19E91 Int File Attributes   0000
      [Bit 0]               0 'Binary Data'
19E93 Ext File Attributes   00000000
19E97 Local Header Offset   00000000
19E9B Filename              'Mean.20160830_073000.zip'

19EB3 CENTRAL HEADER #2     02014B50
19EB7 Created Zip Spec      14 '2.0'
19EB8 Created OS            00 'MS-DOS'
19EB9 Extract Zip Spec      14 '2.0'
19EBA Extract OS            00 'MS-DOS'
19EBB General Purpose Flag  0808
      [Bits 1-2]            0 'Normal Compression'
      [Bit  3]              1 'Streamed'
      [Bit 11]              1 'Language Encoding'
19EBD Compression Method    0008 'Deflated'
19EBF Last Mod Time         491E43F7 'Tue Aug 30 08:31:46 2016'
19EC3 CRC                   BAE824D6
19EC7 Compressed Length     00003FB7
19ECB Uncompressed Length   000043A3
19ECF Filename Length       0018
19ED1 Extra Length          0000
19ED3 Comment Length        0000
19ED5 Disk Start            0000
19ED7 Int File Attributes   0000
      [Bit 0]               0 'Binary Data'
19ED9 Ext File Attributes   00000000
19EDD Local Header Offset   00005012
19EE1 Filename              'Mean.20160830_081500.zip'

19EF9 CENTRAL HEADER #3     02014B50
19EFD Created Zip Spec      14 '2.0'
19EFE Created OS            00 'MS-DOS'
19EFF Extract Zip Spec      14 '2.0'
19F00 Extract OS            00 'MS-DOS'
19F01 General Purpose Flag  0808
      [Bits 1-2]            0 'Normal Compression'
      [Bit  3]              1 'Streamed'
      [Bit 11]              1 'Language Encoding'
19F03 Compression Method    0008 'Deflated'
19F05 Last Mod Time         491E43F7 'Tue Aug 30 08:31:46 2016'
19F09 CRC                   EDC8AE4E
19F0D Compressed Length     00005019
19F11 Uncompressed Length   000054EA
19F15 Filename Length       0018
19F17 Extra Length          0000
19F19 Comment Length        0000
19F1B Disk Start            0000
19F1D Int File Attributes   0000
      [Bit 0]               0 'Binary Data'
19F1F Ext File Attributes   00000000
19F23 Local Header Offset   0000900F
19F27 Filename              'Mean.20160830_071500.zip'

19F3F CENTRAL HEADER #4     02014B50
19F43 Created Zip Spec      14 '2.0'
19F44 Created OS            00 'MS-DOS'
19F45 Extract Zip Spec      14 '2.0'
19F46 Extract OS            00 'MS-DOS'
19F47 General Purpose Flag  0808
      [Bits 1-2]            0 'Normal Compression'
      [Bit  3]              1 'Streamed'
      [Bit 11]              1 'Language Encoding'
19F49 Compression Method    0008 'Deflated'
19F4B Last Mod Time         491E43F7 'Tue Aug 30 08:31:46 2016'
19F4F CRC                   C37437FB
19F53 Compressed Length     000073A0
19F57 Uncompressed Length   00008054
19F5B Filename Length       0018
19F5D Extra Length          0000
19F5F Comment Length        0000
19F61 Disk Start            0000
19F63 Int File Attributes   0000
      [Bit 0]               0 'Binary Data'
19F65 Ext File Attributes   00000000
19F69 Local Header Offset   0000E06E
19F6D Filename              'Mean.20160830_080000.zip'

19F85 CENTRAL HEADER #5     02014B50
19F89 Created Zip Spec      14 '2.0'
19F8A Created OS            00 'MS-DOS'
19F8B Extract Zip Spec      14 '2.0'
19F8C Extract OS            00 'MS-DOS'
19F8D General Purpose Flag  0808
      [Bits 1-2]            0 'Normal Compression'
      [Bit  3]              1 'Streamed'
      [Bit 11]              1 'Language Encoding'
19F8F Compression Method    0008 'Deflated'
19F91 Last Mod Time         491E43F7 'Tue Aug 30 08:31:46 2016'
19F95 CRC                   40E52180
19F99 Compressed Length     000049D3
19F9D Uncompressed Length   00005110
19FA1 Filename Length       0018
19FA3 Extra Length          0000
19FA5 Comment Length        0000
19FA7 Disk Start            0000
19FA9 Int File Attributes   0000
      [Bit 0]               0 'Binary Data'
19FAB Ext File Attributes   00000000
19FAF Local Header Offset   00015454
19FB3 Filename              'Mean.20160830_070000.zip'

19FCB END CENTRAL HEADER    06054B50
19FCF Number of this disk   0000
19FD1 Central Dir Disk no   0000
19FD3 Entries in this disk  0005
19FD5 Total Entries         0005
19FD7 Size of Central Dir   0000015E
19FDB Offset to Central Dir 00019E6D
19FDF Comment Length        0000
Done

解压缩 - 重新压缩文件:

00000 LOCAL HEADER #1       04034B50
00004 Extract Zip Spec      14 '2.0'
00005 Extract OS            00 'MS-DOS'
00006 General Purpose Flag  0000
00008 Compression Method    0000 'Stored'
0000A Last Mod Time         491E510F 'Tue Aug 30 10:08:30 2016'
0000E CRC                   218B9162
00012 Compressed Length     00005595
00016 Uncompressed Length   00005595
0001A Filename Length       0018
0001C Extra Length          0000
0001E Filename              'Mean.20160830_070000.zip'
00036 PAYLOAD

055CB LOCAL HEADER #2       04034B50
055CF Extract Zip Spec      14 '2.0'
055D0 Extract OS            00 'MS-DOS'
055D1 General Purpose Flag  0000
055D3 Compression Method    0000 'Stored'
055D5 Last Mod Time         491E5117 'Tue Aug 30 10:08:46 2016'
055D9 CRC                   180124FD
055DD Compressed Length     00005972
055E1 Uncompressed Length   00005972
055E5 Filename Length       0018
055E7 Extra Length          0000
055E9 Filename              'Mean.20160830_071500.zip'
05601 PAYLOAD

0AF73 LOCAL HEADER #3       04034B50
0AF77 Extract Zip Spec      14 '2.0'
0AF78 Extract OS            00 'MS-DOS'
0AF79 General Purpose Flag  0000
0AF7B Compression Method    0000 'Stored'
0AF7D Last Mod Time         491E511D 'Tue Aug 30 10:08:58 2016'
0AF81 CRC                   03A4486C
0AF85 Compressed Length     00005953
0AF89 Uncompressed Length   00005953
0AF8D Filename Length       0018
0AF8F Extra Length          0000
0AF91 Filename              'Mean.20160830_073000.zip'
0AFA9 PAYLOAD

108FC LOCAL HEADER #4       04034B50
10900 Extract Zip Spec      14 '2.0'
10901 Extract OS            00 'MS-DOS'
10902 General Purpose Flag  0000
10904 Compression Method    0000 'Stored'
10906 Last Mod Time         491E5124 'Tue Aug 30 10:09:08 2016'
1090A CRC                   FEE97172
1090E Compressed Length     00008818
10912 Uncompressed Length   00008818
10916 Filename Length       0018
10918 Extra Length          0000
1091A Filename              'Mean.20160830_080000.zip'
10932 PAYLOAD

1914A LOCAL HEADER #5       04034B50
1914E Extract Zip Spec      14 '2.0'
1914F Extract OS            00 'MS-DOS'
19150 General Purpose Flag  0000
19152 Compression Method    0000 'Stored'
19154 Last Mod Time         491E5129 'Tue Aug 30 10:09:18 2016'
19158 CRC                   0B38337E
1915C Compressed Length     00004713
19160 Uncompressed Length   00004713
19164 Filename Length       0018
19166 Extra Length          0000
19168 Filename              'Mean.20160830_081500.zip'
19180 PAYLOAD

1D893 CENTRAL HEADER #1     02014B50
1D897 Created Zip Spec      14 '2.0'
1D898 Created OS            00 'MS-DOS'
1D899 Extract Zip Spec      14 '2.0'
1D89A Extract OS            00 'MS-DOS'
1D89B General Purpose Flag  0000
1D89D Compression Method    0000 'Stored'
1D89F Last Mod Time         491E510F 'Tue Aug 30 10:08:30 2016'
1D8A3 CRC                   218B9162
1D8A7 Compressed Length     00005595
1D8AB Uncompressed Length   00005595
1D8AF Filename Length       0018
1D8B1 Extra Length          0000
1D8B3 Comment Length        0000
1D8B5 Disk Start            0000
1D8B7 Int File Attributes   0000
  [Bit 0]               0 'Binary Data'
1D8B9 Ext File Attributes   00000020
  [Bit 5]               Archive
1D8BD Local Header Offset   00000000
1D8C1 Filename              'Mean.20160830_070000.zip'

1D8D9 CENTRAL HEADER #2     02014B50
1D8DD Created Zip Spec      14 '2.0'
1D8DE Created OS            00 'MS-DOS'
1D8DF Extract Zip Spec      14 '2.0'
1D8E0 Extract OS            00 'MS-DOS'
1D8E1 General Purpose Flag  0000
1D8E3 Compression Method    0000 'Stored'
1D8E5 Last Mod Time         491E5117 'Tue Aug 30 10:08:46 2016'
1D8E9 CRC                   180124FD
1D8ED Compressed Length     00005972
1D8F1 Uncompressed Length   00005972
1D8F5 Filename Length       0018
1D8F7 Extra Length          0000
1D8F9 Comment Length        0000
1D8FB Disk Start            0000
1D8FD Int File Attributes   0000
  [Bit 0]               0 'Binary Data'
1D8FF Ext File Attributes   00000020
  [Bit 5]               Archive
1D903 Local Header Offset   000055CB
1D907 Filename              'Mean.20160830_071500.zip'

1D91F CENTRAL HEADER #3     02014B50
1D923 Created Zip Spec      14 '2.0'
1D924 Created OS            00 'MS-DOS'
1D925 Extract Zip Spec      14 '2.0'
1D926 Extract OS            00 'MS-DOS'
1D927 General Purpose Flag  0000
1D929 Compression Method    0000 'Stored'
1D92B Last Mod Time         491E511D 'Tue Aug 30 10:08:58 2016'
1D92F CRC                   03A4486C
1D933 Compressed Length     00005953
1D937 Uncompressed Length   00005953
1D93B Filename Length       0018
1D93D Extra Length          0000
1D93F Comment Length        0000
1D941 Disk Start            0000
1D943 Int File Attributes   0000
  [Bit 0]               0 'Binary Data'
1D945 Ext File Attributes   00000020
  [Bit 5]               Archive
1D949 Local Header Offset   0000AF73
1D94D Filename              'Mean.20160830_073000.zip'

1D965 CENTRAL HEADER #4     02014B50
1D969 Created Zip Spec      14 '2.0'
1D96A Created OS            00 'MS-DOS'
1D96B Extract Zip Spec      14 '2.0'
1D96C Extract OS            00 'MS-DOS'
1D96D General Purpose Flag  0000
1D96F Compression Method    0000 'Stored'
1D971 Last Mod Time         491E5124 'Tue Aug 30 10:09:08 2016'
1D975 CRC                   FEE97172
1D979 Compressed Length     00008818
1D97D Uncompressed Length   00008818
1D981 Filename Length       0018
1D983 Extra Length          0000
1D985 Comment Length        0000
1D987 Disk Start            0000
1D989 Int File Attributes   0000
  [Bit 0]               0 'Binary Data'
1D98B Ext File Attributes   00000020
  [Bit 5]               Archive
1D98F Local Header Offset   000108FC
1D993 Filename              'Mean.20160830_080000.zip'

1D9AB CENTRAL HEADER #5     02014B50
1D9AF Created Zip Spec      14 '2.0'
1D9B0 Created OS            00 'MS-DOS'
1D9B1 Extract Zip Spec      14 '2.0'
1D9B2 Extract OS            00 'MS-DOS'
1D9B3 General Purpose Flag  0000
1D9B5 Compression Method    0000 'Stored'
1D9B7 Last Mod Time         491E5129 'Tue Aug 30 10:09:18 2016'
1D9BB CRC                   0B38337E
1D9BF Compressed Length     00004713
1D9C3 Uncompressed Length   00004713
1D9C7 Filename Length       0018
1D9C9 Extra Length          0000
1D9CB Comment Length        0000
1D9CD Disk Start            0000
1D9CF Int File Attributes   0000
  [Bit 0]               0 'Binary Data'
1D9D1 Ext File Attributes   00000020
  [Bit 5]               Archive
1D9D5 Local Header Offset   0001914A
1D9D9 Filename              'Mean.20160830_081500.zip'

1D9F1 END CENTRAL HEADER    06054B50
1D9F5 Number of this disk   0000
1D9F7 Central Dir Disk no   0000
1D9F9 Entries in this disk  0005
1D9FB Total Entries         0005
1D9FD Size of Central Dir   0000015E
1DA01 Offset to Central Dir 0001D893
1DA05 Comment Length        0000
Done

zip文件的结构

  • MainFile.zip
    • InnerFile1.zip
      • InnerFile1.xml
    • InnerFile2.zip
      • InnerFile2.xml
    • InnerFile-N.zip
      • InnerFile-N.zip

创建zip文件的代码

public void addToZip(File zipFile, File... filesToAdd) {
    final byte[] buffer = new byte[1024];

    FileOutputStream fos = new FileOutputStream(zipFile.getAbsoluteFile());
    ZipOutputStream zos = new ZipOutputStream(fos);

    for (File fileToAdd : filesToAdd) {
        ZipEntry entry = new ZipEntry(fileToAdd.getName());
        try {
            zos.putNextEntry(entry);
            try (FileInputStream in = new FileInputStream(input.getAbsoluteFile())) {
                int len;
                while ((len = in.read(buffer)) > 0) {
                    zos.write(buffer, 0, len);
                }
            }
            zos.closeEntry();
        }
    }
    try {
        zos.finish();
        zos.close();
    } catch (IOException ex) {
        LOGGER.log(Level.SEVERE, null, ex);
    }
}

压缩方法

根据the primary reference for Zip files

  

4.1.3数据压缩可用于减少放入ZIP文件的文件大小,但不是必需的。这种格式支持   使用多种数据压缩算法。当压缩时   使用时,必须使用其中一个记录的压缩算法。   建议实施者试验他们的数据   确定哪些可用算法提供最佳   压缩他们的需要。 压缩方法8(Deflate)是   大多数ZIP兼容应用程序默认使用的方法   程序

在这种特定情况下,使用zipEntry.setMethod(ZipEntry.DEFLATED);并不足以让遗留程序接受我的文件。

2 个答案:

答案 0 :(得分:7)

我(几乎)与银行遗留系统有同样的问题。它检查了存档中的Compressed sizeUncompressed size。在您提供的输出中,接受的输出包含有效信息(diff):

00012 Compressed Length     00005595
00012 Compressed Length     00005595

Java生成的存档文件中的字段:

00012 Compressed Length     00000000
00016 Uncompressed Length   00000000

更详细的标题信息结帐Wikipedia

最有可能的问题是:

  

https://stackoverflow.com/a/29096370/4823977

答案 1 :(得分:5)

我讨厌猜测而不能验证我的假设,因此关于FTP服务器上实际服务器端文件检查器的可用性的评论/问题,但是它的价值和假设检查器更喜欢{{ 1}} STORED个条目,以下是创建

的方法
  • 从XML文件中删除ZIP文件(这是有道理的,因为XML可以很好地压缩)和
  • 从嵌套的ZIP文件中存储ZIP文件(这也是有意义的,因为通常压缩文件在重新压缩时不能有效地变小)。

这里的问题是,当使用DEFLATED条目时,还必须为每个条目设置(un-)压缩大小和CRC。这就是你如何做到的(不幸的是我正在读取每个嵌套的ZIP文件两次,一次用于计算CRC,一次用于将其内容复制到ZIP输出流):

驱动程序应用程序:

STORED

ZIP实用程序:

package de.scrum_master.app;

import java.io.File;
import java.io.IOException;
import java.util.SortedMap;
import java.util.TreeMap;

public class Application {
    public static void main(String[] args) throws IOException {
        SortedMap<File, File[]> fileMappings = new TreeMap<>();
        fileMappings.put(
            new File("books.zip"),
            new File[] { new File("books.xml") }
        );
        fileMappings.put(
            new File("cds.zip"),
            new File[] { new File("cds.xml") }
        );
        fileMappings.put(
            new File("clothes.zip"),
            new File[] { new File("clothes.xml") }
        );

        ZipTool zipTool = new ZipTool();
        for (File zipFile : fileMappings.keySet())
            zipTool.addToZip(zipFile, fileMappings.get(zipFile));
        zipTool.addToZip(
            new File("archive.zip"),
            new File[] { new File("books.zip"), new File("cds.zip"), new File("clothes.zip") }
        );
    }
}

也许您可以尝试并向我提供反馈。如果这还不够好并且检查员仍然抱怨,我们可以尝试其他方法使zip档案更加相似。