簡體   English   中英

如何閱讀Zip文件中的評論?

[英]How can I read comments from a Zip file?

如何使用Java在Zip文件中讀寫注釋?

我可以這樣寫評論:

FileOutputStream fos = new FileOutputStream(output);
ZipOutputStream zos = new ZipOutputStream(fos);
zos.setComment("BMC Comment");

嘗試:

java.util.zip.ZipFile.getComment()

檢查這篇文章:

編輯 :不幸的是,現在原始鏈接不可用,這是一個Web存檔鏈接:

http://web.archive.org/web/20100117212418/http://www.flattermann.net:80/2009/01/read-a-zip-file-comment-with-java/

為了后代,這里是要點(略有格式):

private static String getZipCommentFromBuffer (byte[] buffer, int len) {
  byte[] magicDirEnd = {0x50, 0x4b, 0x05, 0x06};
  int buffLen = Math.min(buffer.length, len);

  // Check the buffer from the end
  for (int i = buffLen - magicDirEnd.length - 22; i >= 0; i--) {
    boolean isMagicStart = true;

    for (int k = 0; k < magicDirEnd.length; k++) {
      if (buffer[i + k] != magicDirEnd[k]) {
        isMagicStart = false;
        break;
      }
    }

    if (isMagicStart) {
      // Magic Start found!
      int commentLen = buffer[i + 20] + buffer[i + 21] * 256;
      int realLen = buffLen - i - 22;
      System.out.println ("ZIP comment found at buffer position " 
        + (i + 22) + " with len = " + commentLen + ", good!");

      if (commentLen != realLen) {
        System.out.println ("WARNING! ZIP comment size mismatch: "
          + "directory says len is " + commentLen
          + ", but file ends after " + realLen + " bytes!");
      }

      String comment = new String (buffer, i + 22, Math.min(commentLen, realLen));
      return comment;
    }
  }

  System.out.println ("ERROR! ZIP comment NOT found!");
  return null;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM