2012-09-26 169 views
1

我有以下情況 我要修改現有的文件,並返回一個包含該修改文件的zip,我在web應用上下文中的zip文件 什麼,我做了現在是:創建阿拉伯字符

///////////////// modifying the existing file with poi librairy 
    FileInputStream inpoi = new FileInputStream("file_path"); 
       POIFSFileSystem fs = new POIFSFileSystem(inpoi); 
       HWPFDocument doc = new HWPFDocument(fs); 
       Range r = doc.getRange(); 
       r.replaceText("<nomPrenom>","test"); 
       byte[] b = doc.getDataStream(); 

//////////////////////// create the zip file and copy the modified files into it 
ZipOutputStream out = new ZipOutputStream(new FileOutputStream("my.zip")); 
out.putNextEntry(new ZipEntry("file")); 
for (int j = 0; j < b.length; j++) { 
       out.write(b[j]); 
       } 

創建的壓縮文件不能被正確地字考慮到原始文件在阿拉伯語

wrotten我想這個閱讀:

try { 
       FileInputStream inpoi = new FileInputStream("C:\\Users\\free\\Desktop\\myworkspace\\gestionRH\\WebRoot\\fiches\\blame.doc"); 
        POIFSFileSystem fs = new POIFSFileSystem(inpoi); 
        HWPFDocument doc = new HWPFDocument(fs); 
        Range r = doc.getRange(); 
        r.replaceText("<nomPrenom>","test"); 
        byte[] stream= doc.getDataStream(); 
        String encoding = "utf-16"; 
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream("yyy.zip")); 
        ZipEntry zipEntry = new ZipEntry("file.doc"); 
        OutputStreamWriter writer = new OutputStreamWriter(out,"utf-8"); 
        out.putNextEntry(zipEntry); 
        for (int j = 0; j < stream.length; j++) { 
         writer.write(stream[j]); 
        } 
        writer.close(); 
       } catch (IOException e) { 
        System.out.println(e.toString()); 
       } 

它不工作

+0

看一看這個答案: http://stackoverflow.com/questions/2260325/why-is-java-bufferedreader-not-reading-arabic-and-chinese-characters-correctly – dngfng

+0

我不要認爲這是編碼的問題,因爲我嘗試與法國文件,我認爲這是正確閱讀流的問題 – fatiDev

回答

1

你也可以使用Apache的commons-io的文件實用程序提供了很多方便的方法的Java文件操作 - 讀取和寫入文件等..

讀取和寫入的方法也有一個編碼參數。

http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html

的ZipFile允許您設置正確的編碼。

ZipFile(字符串名稱,字符串編碼) 打開給定文件進行讀取,假定指定了文件名編碼,掃描unicode額外字段。

+0

我不認爲這是編碼的問題,因爲我嘗試與法國文件,我認爲這是正確閱讀流的問題 – fatiDev