Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何防止生成空zip文件_Java_Spring Mvc - Fatal编程技术网

Java 如何防止生成空zip文件

Java 如何防止生成空zip文件,java,spring-mvc,Java,Spring Mvc,我正在生成图像文件的zip。但如果找不到图像,则会生成一个异常,如java.util.zip.ZipException:zip文件必须至少有一个条目。我正在处理异常,但正在生成一个大小为0的空zip。所以请帮我解决这个问题 try { // create the ZIP file ZipOutputStream out = getOutputStream(subpart, destinationZipPath); /*

我正在生成图像文件的zip。但如果找不到图像,则会生成一个异常,如
java.util.zip.ZipException:zip文件必须至少有一个条目
。我正在处理异常,但正在生成一个大小为0的空zip。所以请帮我解决这个问题

    try {
        // create the ZIP file

        ZipOutputStream out = getOutputStream(subpart, destinationZipPath);

        /*
         * ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
         * destinationZipPath));
         */
        // compress the files
        LOGGER.error("zip creation is started @" + new Date().toString());
        for (String fileNameDB : filesTobeZipped) {
            // To check duplication of filename in zip creation
            if (!filesHash.containsKey(fileNameDB)) {
                filesHash.put(fileNameDB, fileNameDB);
                File f = new File(sourceFolder + fileNameDB);
                // to chk file is exists on physical location or not
                if (f.exists()) {
                    if (fileCount >= batchFileLimit) {
                        out.close();
                        subpart++;
                        out = getOutputStream(subpart, destinationZipPath);
                        fileCount = 0;
                        // overallSize=0;
                    }
                    FileInputStream in = new FileInputStream(f);
                    // add ZIP entry to output stream
                    out.putNextEntry(new ZipEntry(f.getName()));
                    // transfer bytes from the file to the ZIP file
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    // complete the entry
                    out.closeEntry();
                    in.close();
                    fileCount++;
                } else {
                }
            }

        }
        // complete the ZIP file
        out.close(); // Exception if fileCount=0;
        return true;
        // return zipfile;
    } catch (IOException ex) {
        return false;
    }

您不能在检测到第一个现有文件后创建ZIP流吗。就像

ZipOutputStream out = null;

for (String fileNameDB : filesTobeZipped) {
    if (new File(fileNameDB).exists()) {
        if (out == null) {
            out= ZipOutputStream out = getOutputStream(subpart, destinationZipPath);
        }

        // do other operations
    }
}

你可以这样做:

ZipOutputStream out = null;
try {
    out = getOutputStream(subpart, destinationZipPath);
    ...
    out.close(); // Exception if fileCount=0;
    return true;
    // return zipfile;
} catch (IOException ex) {
    if (out != null) {
        out.close();
    }
    destinationZipPath.toFile().delete(); // Or whatever is appropriate.
    return false;
}
也许更好的办法是在循环中试一试。以及检查文件的数量

然后,您可以尝试使用资源