簡體   English   中英

如何讀取zip文件中的htm文件?

[英]how to read htm file inside a zip file?

我有一個包含Index.htm的zip文件。 我應該閱讀Index.htm的內容,並在其中找到一個日期( 2011年12月 ),並使用該日期創建一個目錄,然后在該目錄中提取zip文件。

這是html文件:

<HTML>    
  <HEAD></HEAD>    
  <BODY>    
  <A Name="TopOfPage"></A>    
  <TABLE Width="100%" Border="0" CellPadding="0" CellSpacing="0">    
   <TR> 
     <TD Width="30%"><A HRef="HeaderTxt/HetBCFI.htm">Het B.C.F.I.</A></TD>    
   </TR>      
  </TABLE>    
  <TABLE Width="100%" Border="0" CellPadding="0" CellSpacing="0">
   <TR> 
    <TD RowSpan="2" Width="10"></TD>
    <TD Width="70%"><STRONG><FONT Face="Arial" Size="2">Gecommentarieerd   Geneesmiddelenrepertorium</FONT></STRONG></TD> 
    <TD Width="29%" Align="Right" Class= "Datum">&nbsp;
   December 2011&nbsp;&nbsp;
  </TD>
  <TD Rowspan="2" Width="10"></TD>
 </TR>
</TABLE> </BODY> </HTML>

嘗試這個,

  1. 使用java.util.zip包讀取html
  2. 使用一些html解析器(我建議使用JSoup )來獲取日期字符串。 這是對您有幫助的鏈接

獲得日期字符串后,創建所需的目錄。

編輯 :要刪除&nbsp; ,您可以執行以下操作之一,

  • 使用包含&nbsp;的字符串創建另一個文檔元素&nbsp; 並執行以下操作

    document.select(":containsOwn(\ )").remove(); (從這里拍攝)

  • 使用關注(假設您要清除的字符串是htmlString

    Jsoup.parse(htmlString).text();

  • 使用String的replaceAll()函數擺脫&nbsp;

幾個步驟:

  1. 使用java.util.zip包並創建一個解壓縮的流。
  2. 使用XML解析器(如JSoup)遍歷節點,然后...
  3. 使用正則表達式或帶有日期解析器的正則表達式(例如SimpleDateFormat)來選擇日期。

這假定您要查找的日期始終在文本節點中。

這是我使用的正確的最終代碼:多謝您提供有用的提示

public static String getDateWithinHtmlInsideZipFile(File archive) {
      ZipFile zp = new ZipFile(archive);
      InputStream in = zp.getInputStream (zp.getEntry ("Index.htm"));

      Document doc = Jsoup.parse(in, "UTF-8", "");

    return doc.body().getElementsByClass("Datum").text().trim();
}

暫無
暫無

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

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