简体   繁体   English

Spawned zip进程似乎没有添加文件存档

[英]Spawned zip process doesn't seem to add file to archive

I am trying to add a text file to a zip archive through a Java program on Linux. 我试图通过Linux上的Java程序将文本文件添加到zip存档。 The program spawns a process (using java.lang.Process) to execute the commandline "zip -j .zip .txt", reads the output and error streams of the spawned process and waits for the process to complete using waitFor(). 该程序生成一个进程(使用java.lang.Process)来执行命令行“zip -j .zip .txt”,读取生成进程的输出和错误流,并等待使用waitFor()完成进程。 Though the program seems to run fine (spawned process exits with exit code 0, indicating that the zip commandline was executed successfully) and the output read from output and error streams do not indicate any errors, at the end of the program the zip archive doesn't always contain the file supposed to have been added. 虽然程序似乎运行良好(生成的进程退出,退出代码为0,表示zip命令行已成功执行)并且输出和错误流的输出读数未指示任何错误,但在程序结束时,zip存档不会总是包含应该添加的文件。 This problem doesn't happen consistently though (even with the same existing-archive and file-to-add) - once in a while (perhaps once in 4 attempts) the zip is found to have been updated correctly. 这个问题不会一直发生(即使使用相同的现有存档和文件添加) - 偶尔(可能一次尝试4次),发现zip已正确更新。 Strangely, the problem doesn't occur at all when the program is run through Eclipse debugger mode. 奇怪的是,当程序通过Eclipse调试器模式运行时,问题根本不会发生。 Any pointers on why this problem occurs and how it can be addressed would be helpful. 关于为什么会出现这个问题以及如何解决这个问题的任何指示都会有所帮助。 Thanks! 谢谢!

Below is the code snippet. 以下是代码段。 The program calls addFileToZip(File, File, String): 该程序调用addFileToZip(File,File,String):

public static void addFileToZip(final File zipFile, final File fileToBeAdded,
        final String fileNameToBeAddedAs) throws Exception {
    File tempDir = createTempDir();
    File fileToBeAddedAs = new File(tempDir, fileNameToBeAddedAs);
    try {
        FileUtils.copyFile(fileToBeAdded, fileToBeAddedAs);
        addFileToZip(zipFile, fileToBeAddedAs);
    } finally {
        deleteFile(fileToBeAddedAs);
        deleteFile(tempDir);
    }
}

public static void addFileToZip(final File zipFile, final File fileToBeAdded) throws Exception {
    final String[] command = {"zip", "-j", zipFile.getAbsolutePath(), fileToBeAdded.getAbsolutePath()};
    ProcessBuilder procBuilder = new ProcessBuilder(command);
    Process proc = procBuilder.start();
    int exitCode = proc.waitFor();
    /*
     * Code to read output/error streams of proc and log/print them
     */
    if (exitCode != 0) {
            throw new Exception("Unable to add file, error: " + errMsg);
    }
}

Make sure no other process has the zip file locked for write, or the file being added locked for read. 确保没有其他进程锁定zip文件以进行写入,或者将文件添加为已锁定以供读取。 If you're generating the file to be added, make sure the stream is flushed and closed before spawning the zip utility. 如果要生成要添加的文件,请确保在生成zip实用程序之前刷新并关闭流。

I am trying to add a text file to a zip archive through a Java program on Linux. 我试图通过Linux上的Java程序将文本文件添加到zip存档。

Use the java.util.zip API , which: 使用java.util.zip API ,其中:

Provides classes for reading and writing the standard ZIP and GZIP file formats. 提供用于读取和写入标准ZIP和GZIP文件格式的类。


If you intend to stick with using a Process to do this, be sure to implement all the suggestions of When Runtime.exec() won't . 如果您打算坚持使用Process来执行此操作,请确保实现Runtime.exec()不会的 所有建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM