Java 保存/读取ZIP中的xml内容,并保持正确的编码

Java 保存/读取ZIP中的xml内容,并保持正确的编码,java,xml,encoding,zip,Java,Xml,Encoding,Zip,桌面应用程序将其数据存储在xml文件中。此xml文件与其他文件一起存储在Zip存档中,以保持其状态。我一直对编码非常反感,但这次我真的不明白,为什么它不起作用。以下问题: 当我将数据持久化到xml文件中时,一切看起来都很好。我记录输出,所有编码都正确。我可以用其他工具打开Zip来检查XML,编码也很好,但是当我在Java应用程序中再次尝试读取它时,编码就被弄乱了,比如说,德国的UMLAUT不再正确 以下代码用于从zip读取xml: private String readZipArchive(

桌面应用程序将其数据存储在xml文件中。此xml文件与其他文件一起存储在Zip存档中,以保持其状态。我一直对编码非常反感,但这次我真的不明白,为什么它不起作用。以下问题:

当我将数据持久化到xml文件中时,一切看起来都很好。我记录输出,所有编码都正确。我可以用其他工具打开Zip来检查XML,编码也很好,但是当我在Java应用程序中再次尝试读取它时,编码就被弄乱了,比如说,德国的UMLAUT不再正确

以下代码用于从zip读取xml:

  private String readZipArchive( final Path path ) throws ZipException, IOException
  {
    String xmlData = null;

    try (ZipFile zipFile = new ZipFile( path.toFile(), StandardCharsets.UTF_8 ))
    {
      final Enumeration<? extends ZipEntry> zipEntryEnum = zipFile.entries();

      while ( zipEntryEnum.hasMoreElements() )
      {
        final ZipEntry zipEntry = zipEntryEnum.nextElement();

        logger.debug( "zipEntry: " + zipEntry + " comment: " + zipEntry.getComment() );

        switch ( FileType.valueOf( zipEntry.getComment() ) )
        {
          case DATA:

            xmlData = convertStreamToString( zipFile.getInputStream( zipEntry ) );

            //Here the String is not UTF 8, why? German Umlauts are broken:
            logger.dev( "Load State from File: \n" + xmlData ); 

            break;

          case PICTURE:
            //OTHER Implementation, not important.
            break;
        }
      }

      return xmlData;
    }
  }


  private static String convertStreamToString( final InputStream is )
  {
    try (Scanner s = new Scanner( is, "UTF-8" ))
    {
      s.useDelimiter( "\\A" );
      return s.hasNext() ? s.next() : "";
    }
  }
私有字符串readZipArchive(最终路径)抛出ZipException、IOException
{
字符串xmlData=null;
try(ZipFile-ZipFile=new-ZipFile(path.toFile(),StandardCharsets.utf8))
{

最终枚举我认为问题在于记录器没有输出UTF-8

检查记录器是否将UTF-8记录为其系统输出


java.util.logging.ConsoleHandler.encoding=UTF8

不幸的是,编码已经正确设置,以及工作区的eclipse编码设置。因此,我仍然猜测在从ZIP读取内容时一定会出现错误。不过,很可能是我在错误的位置搜索了,因此感谢您与我们分享r的想法。