zip格式分析

來源:互聯網
上載者:User

從網上下載的一個介紹zip壓縮包格式的詳細分析檔案,內容如下。其實解壓zip包的過程是從中央目錄結構 讀取檔案的檔案標題位置,接著讀轉到標題繼續向下讀取檔案資料,將此資料傳給解壓介面進行解壓。

Files stored in arbitrary order.  Large .ZIP files can span multiple

volumes or be split into user-defined segment sizes. All values
are stored in little-endian byte order unless otherwise specified.

總覽 .zip 檔案格式:
[檔案標題 1]
[檔案資料 1]
[資料描述符記錄 1]
.
.
.
[檔案標題 n]
[檔案資料 n]
[資料描述符記錄 n]
[歸檔解密標題]
[歸檔額外資料記錄]
[中央目錄結構]
[中央目錄記錄的 Zip64 結尾]
[中央目錄定位器的 Zip64 結尾]
[中央目錄記錄的結尾]

- A 檔案標題 --------------------

檔案標題簽名       4 位元組 [開始 0] (0x04034b50)
所需版本           2 位元組 [開始 4]
一般用途位標記     2 位元組 [開始 6]
壓縮方法           2 位元組 [開始 8] (8=DEFLATE; 0=UNCOMPRESSED)
檔案的最後修改時間 2 位元組 [開始 10]
檔案的最後修改日期 2 位元組 [開始 12]
crc-32             4 位元組 [開始 14]
壓縮後的大小       4 位元組 [開始 18]
解壓縮後的大小     4 位元組 [開始 22]
檔案名稱長度         2 位元組 [開始 26]
額外欄位長度       2 位元組 [開始 28]

檔案名稱             變數
額外欄位           變數

- B 檔案資料 --------------------

緊接著檔案標題就是已壓縮的或未壓縮的檔案資料。
在 zip 歸檔裡面的每一個檔案都是一系列重複的
[檔案標題][檔案資料][資料描述符記錄]。

- C 資料描述符記錄 --------------------

  crc-32         4 位元組 [開始 0]
  壓縮後的大小   4 位元組 [開始 4]
  解壓縮後的大小 4 位元組 [開始 8]

      This descriptor exists only if bit 3 of the general
      purpose bit flag is set (see below).  It is byte aligned
      and immediately follows the last byte of compressed data.
      This descriptor is used only when it was not possible to
      seek in the output .ZIP file, e.g., when the output .ZIP file
      was standard output or a non-seekable device.  For ZIP64(tm) format
      archives, the compressed and uncompressed sizes are 8 bytes each.

      When compressing files, compressed and uncompressed sizes
      should be stored in ZIP64 format (as 8 byte values) when a
      files size exceeds 0xFFFFFFFF.   However ZIP64 format may be
      used regardless of the size of a file.  When extracting, if
      the zip64 extended information extra field is present for
      the file the compressed and uncompressed sizes will be 8
      byte values.  

      Although not originally assigned a signature, the value
      0x08074b50 has commonly been adopted as a signature value
      for the data descriptor record.  Implementers should be
      aware that ZIP files may be encountered with or without this
      signature marking data descriptors and should account for
      either case when reading ZIP files to ensure compatibility.
      When writing ZIP files, it is recommended to include the
      signature value marking the data descriptor record.  When
      the signature is used, the fields currently defined for
      the data descriptor record will immediately follow the
      signature.

      An extensible data descriptor will be released in a future
      version of this APPNOTE.  This new record is intended to
      resolve conflicts with the use of this record going forward,
      and to provide better support for streamed file processing.

      When the Central Directory Encryption method is used, the data
      descriptor record is not required, but may be used.  If present,
      and bit 3 of the general purpose bit field is set to indicate
      its presence, the values in fields of the data descriptor
      record should be set to binary zeros.

- D 歸檔解密標題 --------------------

      The Archive Decryption Header is introduced in version 6.2
      of the ZIP format specification.  This record exists in support
      of the Central Directory Encryption Feature implemented as part of
      the Strong Encryption Specification as described in this document.
      When the Central Directory Structure is encrypted, this decryption
      header will precede the encrypted data segment.  The encrypted
      data segment will consist of the Archive extra data record (if
      present) and the encrypted Central Directory Structure data.
      The format of this data record is identical to the Decryption
      header record preceding compressed file data.  If the central
      directory structure is encrypted, the location of the start of
      this data record is determined using the Start of Central Directory
      field in the Zip64 End of Central Directory record.  Refer to the
      section on the Strong Encryption Specification for information
      on the fields used in the Archive Decryption Header record.

- E 歸檔額外資料記錄 --------------------

  歸檔額外資料簽名 4 位元組 [開始 0] (0x08064b50)
  額外欄位長度     4 位元組 [開始 4]
  額外欄位         4 位元組 [開始 8]

      The Archive Extra Data Record is introduced in version 6.2
      of the ZIP format specification.  This record exists in support
      of the Central Directory Encryption Feature implemented as part of
      the Strong Encryption Specification as described in this document.
      When present, this record immediately precedes the central
      directory data structure.  The size of this data record will be
      included in the Size of the Central Directory field in the
      End of Central Directory record.  If the central directory structure
      is compressed, but not encrypted, the location of the start of
      this data record is determined using the Start of Central Directory
      field in the Zip64 End of Central Directory record.  

- F 中央目錄結構 --------------------

[檔案標題 1]
.
.
.
[檔案標題 n]
[數位簽章]

檔案標題:
  中央檔案標題簽名   4 位元組 [開始 0] (0x02014b50)
  version made by    2 位元組 [開始 4]
  所需版本           2 位元組 [開始 6]
  一般用途位標記     2 位元組 [開始 8]
  壓縮方法           2 位元組 [開始 10] (8=DEFLATE; 0=UNCOMPRESSED)
  檔案的最後修改時間 2 位元組 [開始 12]
  檔案的最後修改日期 2 位元組 [開始 14]
  crc-32             4 位元組 [開始 16]
  壓縮後的大小       4 位元組 [開始 20]
  解壓縮後的大小     4 位元組 [開始 24]
  檔案名稱長度         2 位元組 [開始 28]
  額外欄位長度       2 位元組 [開始 30]
  檔案注釋長度       2 位元組 [開始 32]
  磁碟開始號         2 位元組 [開始 34]
  內部檔案屬性       2 位元組 [開始 36]
  外部檔案屬性       4 位元組 [開始 38]
  相關的標題位移量   4 位元組 [開始 42]
 
  檔案名稱             變數
  額外欄位           變數
  檔案注釋           變數

數位簽章:
  標題簽名 4 位元組 [開始 0] (0x05054b50)
  資料大小 2 位元組 [開始 4]
  簽名資料 變數

      With the introduction of the Central Directory Encryption
      feature in version 6.2 of this specification, the Central
      Directory Structure may be stored both compressed and encrypted.
      Although not required, it is assumed when encrypting the
      Central Directory Structure, that it will be compressed
      for greater storage efficiency.  Information on the
      Central Directory Encryption feature can be found in the section
      describing the Strong Encryption Specification. The Digital
      Signature record will be neither compressed nor encrypted.

- G 中央目錄記錄的 Zip64 結尾 --------------------

中央目錄的 Zip64 結尾簽名       4 位元組 [開始 0] (0x06064b50)
中央目錄記錄的 Zip64 結尾大小   8 位元組 [開始 4]
version made by                 2 位元組 [開始 12]
所需版本                        2 位元組 [開始 14]
磁碟數目                        4 位元組 [開始 16]
number of the disk with the
 start of the central directory 4 位元組 [開始 20]
total number of entries in the
 central directory on this disk 8 位元組 [開始 24]
中央目錄入口總數                8 位元組 [開始 32]
中央目錄的大小                  8 位元組 [開始 40]
offset of start of central
 directory with respect to
 the starting disk number       8 位元組 [開始 48]
zip64 extensible data sector    變數   [開始 56]

        The value stored into the "size of zip64 end of central
        directory record" should be the size of the remaining
        record and should not include the leading 12 bytes.
 
        Size = SizeOfFixedFields + SizeOfVariableData - 12.

        The above record structure defines Version 1 of the
        zip64 end of central directory record. Version 1 was
        implemented in versions of this specification preceding
        6.2 in support of the ZIP64 large file feature. The
        introduction of the Central Directory Encryption feature
        implemented in version 6.2 as part of the Strong Encryption
        Specification defines Version 2 of this record structure.
        Refer to the section describing the Strong Encryption
        Specification for details on the version 2 format for
        this record.

        Special purpose data may reside in the zip64 extensible data
        sector field following either a V1 or V2 version of this
        record.  To ensure identification of this special purpose data
        it must include an identifying header block consisting of the
        following:

           Header ID  -  2 bytes
           Data Size  -  4 bytes

        The Header ID field indicates the type of data that is in the
        data block that follows.

        Data Size identifies the number of bytes that follow for this
        data block type.

        Multiple special purpose data blocks may be present, but each
        must be preceded by a Header ID and Data Size field.  Current
        mappings of Header ID values supported in this field are as
        defined in APPENDIX C.

- H 中央目錄定位器的 Zip64 結尾 --------------------

中央目錄定位器的 Zip64 結尾簽名       4 位元組 [開始 0] (0x07064b50)
number of the disk with the
 start of the zip64 end of
 central directory                    4 位元組 [開始 4]
相關的中央目錄記錄的 Zip64 結尾位移量 8 位元組 [開始 8]
磁碟總數                              4 位元組 [開始 16]

- I 中央目錄記錄的結尾 --------------------

中央目錄記錄簽名                4 位元組 [開始 0] (0x06054b50) 註:使用“冒泡”從檔案尾追查上來,找到這個簽名。
磁碟編號                        2 位元組 [開始 4]
中央目錄開始磁碟編號            2 位元組 [開始 6]
本磁碟上在中央目錄裡的入口總數  2 位元組 [開始 8]
中央目錄裡的入口總數            2 位元組 [開始 10] 註:檔案總數,檔案夾也算一個檔案。
中央目錄的大小                  4 位元組 [開始 12]
中央目錄對第一張磁碟的位移量    4 位元組 [開始 16]
.ZIP 檔案注釋長度               2 位元組 [開始 20]
.ZIP 檔案注釋                   變數   [開始 22] 註:此處需要使用ByteArray.readMultiByte(),在第二個參數裡指明“gb2312”才能支援中文。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.