1

i have the following situation i have to modify an existing files and return a zip containing this modified files , i'm in web application context what i done up to now is :

///////////////// 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]);
              } 

the created zipped file can't be read correctly with word given that the original file is wrotten in arabic

i tried to this :

 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());
                }

it doesn't work

fatiDev
  • 5,752
  • 7
  • 28
  • 45
  • Have a look at this answer: http://stackoverflow.com/questions/2260325/why-is-java-bufferedreader-not-reading-arabic-and-chinese-characters-correctly – dngfng Sep 26 '12 at 09:31
  • i don't think it's matter of encoding because i tried with french files , i think it's problem of reading stream properly – fatiDev Sep 26 '12 at 09:45

2 Answers2

2

There was an old bug in java zip implementation. It should be fixed in v7. http://bugs.sun.com/view_bug.do?bug_id=4244499

OkieOth
  • 3,604
  • 1
  • 19
  • 29
  • btw what build number has your java? – OkieOth Sep 26 '12 at 12:12
  • what do you mean by build number ? – fatiDev Sep 26 '12 at 14:02
  • If I run 'java -version' I got something like that ' ... Java(TM) SE Runtime Environment (build 1.7.0-b147)'. IMO b147 is the build. The bug database tells since b57 it should be fixed. So it should work on my machine. I will test it next days. – OkieOth Sep 26 '12 at 14:43
1

You may also want to use apache commons-io FileUtils offer alot of handy methods for Java File operation - reading and writing files etc...

The read and write Methods also have an encoding parameters.

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

ZipFile allows you to set the proper encoding.

ZipFile(String name, String encoding) Opens the given file for reading, assuming the specified encoding for file names, scanning unicode extra fields.

dngfng
  • 1,923
  • 17
  • 34
  • i don't think it's matter of encoding because i tried with french files , i think it's problem of reading stream properly – fatiDev Sep 26 '12 at 09:45