UTF-8 source file. Need to create a zip archive. Archiving as follows:

FileInputStream byteArrayInputStream = new FileInputStream("C:\\input.xml"); byte[] buffer = new byte[1024]; FileOutputStream baos = new FileOutputStream("C:\\output.xml"); ZipArchiveOutputStream zos = new ZipArchiveOutputStream(baos); ZipArchiveEntry ze = new ZipArchiveEntry(fileName); zos.putArchiveEntry(ze); InputStream in = byteArrayInputStream; int len; while ((len = in.read(buffer)) > 0) zos.write(buffer, 0, len); in.close(); zos.closeArchiveEntry(); zos.close(); baos.close(); byteArrayInputStream.close(); 

When you open an archived xml in a text editor (for example, notepad ++), incorrect content is opened, for example: <Вх_ВЗЛ:xd0?сточникДанных>ЗЛ</Вх_ВЗЛ:xd0?сточникДанных> i.e. instead of "and" incomprehensible characters

How to fix it

  • ZipArchiveOutputStream creates a .zip file, i.e. The archive (it can be opened with a guide or archiver) containing the ZipArchiveEntry file (with the name from fileName ) is not very clear why you save it as .xml, and how you open it. D0 is the first byte "And" in utf-8, the second byte is there 98 . Is the source file normal? The feeling that someone is reading the file in blocks of N bytes, recodes the block, and pastes the results, so when a multibyte character hits the block boundary, it turns out to be garbage. - zRrr

2 answers 2

Try this:

 zos.setEncoding("UTF-8"); zos.setFallbackToUTF8(true); // для неизвестных симовлов zos.setUseLanguageEncodingFlag(true); zos.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS); 
  • did not help. the result is the same - Dmitry
  • What encoding is displayed as a result? - Mstislav Pavlov

the question was solved with this code, i.e. without forced installation of UTF-8

 zos.setFallbackToUTF8(true); zos.setUseLanguageEncodingFlag(true); zos.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS);