zip文件的内容写得不正确

时间:2015-10-06 18:56:15

标签: java file zip

我正在读取zip文件的内容,当我找到sample.xml文件时,我编辑它的内容并写入输出zip文件

public class CopyEditZip {

static String fileSeparator = System.getProperty("file.separator");

    public static void main(String[] args) {
        System.getProperty("file.separator");

        ZipFile zipFile;
        try {
             zipFile = new ZipFile("c:/temp/source.zip);
             ZipOutputStream zos = new ZipOutputStream(new 
                                   FileOutputStream(
                                          c:/temp/target.zip));

             for (Enumeration e = zipFile.entries(); 
                        e.hasMoreElements();) 
                 {
                    ZipEntry entryIn = (ZipEntry) e.nextElement();
                    if (entryIn.getName().contains("sample.xml")) {
                        zos.putNextEntry(new ZipEntry("sample.xml"));
                        InputStream is = zipFile.getInputStream(entryIn);
                        byte[] buf = new byte[1024];
                        int len;
                        while ((len = (is.read(buf))) > 0) {
                            String x = new String(buf);
                            if (x.contains("Input")) {
                               System.out.println("edit count");
                                x = x.replace("Input", "output");
                            }
                            buf = x.getBytes();
                            zos.write(buf, 0, (len < buf.length) ? len
                                : buf.length);
                        }
                        is.close();
                        zos.closeEntry();
                 }
                zos.close();
                zipFile.close();
          } catch (Exception ex) {

        }

       }
     }

现在输出中的sample.xml没有正确显示。有些数据被截断,有些数据丢失。这是否与缓冲区无法正确写入有关?编辑文件并将其写出的任何其他替代方法?

编辑:我看到xml正在编写,之后是来自xml的更多数据。 mt end标签被称为broker,然后是几行数据。不确定它是如何在结束标记之后写入更多数据的。

编辑:

我逐行放置一个计数器和sysout,看看在while循环的每次迭代过程中出现了什么。

这是最后两行

18
put.fileFtpDirectory"/><ConfigurableProperty uri="CDTSFileInput#File     
Input.fileFtpServer"/><ConfigurableProperty uri="CDTSFileInput#File     
Input.fileFtpUser"/><ConfigurableProperty uri="CDTSFileInput#File 
Input.longRetryInterval"/><ConfigurableProperty uri="CDTSFileInput#File 
Input.messageCodedCharSetIdProperty"/><ConfigurableProperty 
uri="CDTSFileInput#File Input.messageEncodingProperty"/>
<ConfigurableProperty uri="CDTSFileInput#File Input.remoteTransferType"/>
<ConfigurableProperty uri="CDTSFileInput#File Input.retryThreshold"/>
<ConfigurableProperty uri="CDTSFileInput#File Input.shortRetryInterval"/>
<ConfigurableProperty uri="CDTSFileInput#File Input.validateMaster"/>
<ConfigurableProperty override="30" uri="CDTSFileInput#File 
Input.waitInterval"/><ConfigurableProperty override="no" 
uri="CDTSFileInput#FileInput.connectDatasourceBeforeFlowStarts"/>
<ConfigurableProperty uri="CDTSFileInput#FileInput.validateMaster"/>
<ConfigurableProperty override="/apps/cdts/trace/ExceptionTrace-
CDTSFileInput-CDT.REF_EXT.Q01.txt" uri="CDTSFileInp

19
ut#FilePath_ExceptionTrace"/><ConfigurableProperty   
override="/apps/cdts/trace/SnapTrace-CDTSFileInput-CDT.REF_EXT.Q01.txt"    
uri="CDTSFileInput#FilePath_SnapTraceENV"/><ConfigurableProperty    
override="/apps/cdts/trace/SnapTrace-CDTSFileInput-CDT.REF_EXT.Q01.txt" 
uri="CDTSFileInput#FilePath_SnapTraceNOENV"/><ConfigurableProperty   
override="EXTERNAL" uri="CDTSFileInput#INPUTORIGIN"/>
<ConfigurableProperty 
override="/apps/cdts/data_in/data_in_fileinput_gtr1" 
uri="CDTSFileInput#InputDirectory"/><ConfigurableProperty override="GTR" 
uri="CDTSFileInput#SUBMITTERID"/><ConfigurableProperty 
override="FILEINPT" uri="CDTSFileInput#SUBMITTERTYPE"/>
<ConfigurableProperty override="" uri="CDTSFileInput#excludePattern"/>
<ConfigurableProperty override="*" uri="CDTSFileInput#filenamePattern"/>
<ConfigurableProperty override="no" 
uri="CDTSFileInput#recursiveDirectories"/></CompiledMessageFlow>
</Broker>ileInput#FileInput.validateMaster"/><ConfigurableProperty 
override="/apps/cdts/trace/ExceptionTrace-CDTSFileInput-
CDT.REF_EXT.Q01.txt" uri="CDTSFileInp

xml结束,但是最后一行的一部分再次附加。

1 个答案:

答案 0 :(得分:2)

阅读和撰写文字

如果文件是文本文件,则不应将其作为字节读取。你应该用读取器包装输入流,读取行,然后将它们写回到围绕输出流的写入器。

其中一个原因是该文件可能采用非单字节编码,如UTF-8。这意味着一个字符可以在一个缓冲区和下一个缓冲区之间分割。

另一个问题是单词Input可能在缓冲区之间分开。因此,您可能会在其中一个Inp和下一个ut,但您将无法正确匹配。阅读线条是确保您不会停留在单词中间的好方法。

但是,使用ZipOutputStream编写文本并不简单,因为没有为每个条目获取单独的输出流。因此,您需要从您读取的行中提取字节,并将其写入zip文件 - 就像您一样。

读取和写入字节

即使文件恰好是ASCII格式,您的读/写循环也会遇到一些问题。第一个,次要的是你的循环条件应该是:

((len = (is.read(buf)) >= 0)

你真的应该只在获得-1时终止循环。理论上,如果缓冲区大小为零,您可以在循环中间读取根本没有读取任何字节的读取,但这并不意味着流已结束。所以>=,而不是>

但是,更糟糕的问题是您读取len个字节,但是将整个缓冲区转换为字符串。因此,如果你有一个1024字节的缓冲区,len只有50,那么只有50字节的缓冲区将是最新读取的内容,其余的将来自上一次读取,或者是零。

因此,如果这是您所读取的内容,请始终使用len个字节。你应该使用

String x = new String(buf,0,len);

而不是

String x = new String(buf);

此外,您应该注意:

buf = x.getBytes();

您的缓冲区不再是1024字节长。如果最初有1024个字节,并且您的字符串中有10 Input次出现,则缓冲区现在将为1034字节长(假设采用单字节编码)。 len已不再相关 - 它将小于数字。这就是为什么你有丢失的角色的另一个原因。

<强>编码

通常,XML文件是UTF-8。将字节转换为字符串时反之亦然,反之亦然,以及创建读者和编写者时,必须明确说明编码。否则,可能会不恰当地读取字符。

<强>摘要

  • 首选文本文件的基于行的读取循环。
  • 如果您读取字节而不是行:如果您读取len个字节,请使用len个字节,而不是整个缓冲区。
  • 如果更改数据,请不要使用旧的len。
  • 使用编码。

所以新循环的草图将是:

for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements();) {
    ZipEntry entryIn = e.nextElement();
    if (entryIn.getName().contains("sample.xml")) {
        zos.putNextEntry(new ZipEntry("sample.xml"));
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(zipFile.getInputStream(entryIn),
                                                                                      StandardCharsets.UTF_8))) {
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains("Input")) {
                    System.out.println("edit count");
                    line = line.replace("Input", "output")

                }
                line += System.lineSeparator(); // Add newline back.
                byte[] buf = line.getBytes(StandardCharsets.UTF_8);
                zos.write(buf);
            }
        }
     zos.closeEntry();
    }
}

注意:

  • 尝试使用资源打开缓冲的阅读器。它将自动关闭(使用其基础读取器和输入蒸汽)。
  • 请勿使用原始类型Enumeration。使用正确的通配符,您将能够避免显式转换。
  • 由于您从整行创建缓冲区,并且只有该行,您可以编写该完整缓冲区,并且不需要偏移和长度。