Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.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_Zip - Fatal编程技术网

Java 生成一个空的zip文件

Java 生成一个空的zip文件,java,zip,Java,Zip,我目前正在编写一个函数,该函数将创建一个zip文件,该文件将用于其他功能。下面是我的函数代码: public void createZip(){ try{ String outfile = this.filename + ".zip"; //input file FileInputStream input = new FileInputStream(this.filename); /

我目前正在编写一个函数,该函数将创建一个zip文件,该文件将用于其他功能。下面是我的函数代码:

public void createZip(){

        try{
            String outfile = this.filename + ".zip";
            //input file
            FileInputStream input = new FileInputStream(this.filename);
            //output file
            ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(outfile));
            //name the file inside the zip file
            System.out.println(this.filename);
            zip.putNextEntry(new ZipEntry(this.filename));

            byte[] buffer = new byte[this.BUFFER];
            int len;
            //copy the file to the zip
            while((len= input.read(buffer)) > 0){
                System.out.println(len);
                zip.write(buffer, 0, len);
            }
            zip.closeEntry();
            zip.flush();
            input.close();
            zip.close();
            this.filename += ".zip";
        }
        catch(IOException e){
            e.printStackTrace();
        }

    }

我已尝试调试,但找不到此问题的根源。函数运行时没有任何其他问题,但生成的zip文件是空的。

在关闭前尝试使用zip.flush()刷新缓冲区,尽管close应该刷新缓冲区


还要验证此文件名。您有一个同名的局部变量。从不使用局部变量filename。zip文件可能被写入的位置与您预期的不同

必须在关闭输出流之前使用关闭条目,否则该条目永远不会被确认为已完全写入


此外,ZipEntry的名称不能是整个路径;i、 e,它应该是
dog.png
而不是
C:\Users\Admin\Documents\dog.png
。此问题将毫无例外地通过,并将导致文件中的数据直接压缩到zip中,而不是作为压缩文件压缩到zip中。

好吧,我只是想知道,如果在代码中不使用文件名,为什么要将其作为参数传递。。 因为您总是使用this.filename。这让我觉得您正在尝试使用设置为“对象”状态的名称命名zip文件,并且由于您在Zippentry中也使用了相同的名称,因此尝试在其中添加相同的zipper文件。。由于ZipEntry必须指向一个现有的文件,因此它会显示为空


希望有帮助。

@phillipe请试试这个

    public void createZip(String filename) {
    try {
        //input file
        FileInputStream input = new FileInputStream(filename);
        //output file
        ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(filename + ".zip"));
        //name the file inside the zip file
        zip.putNextEntry(new ZipEntry(filename));

        byte[] buffer = new byte[1024];
        int len;
        //copy the file to the zip
        while((len = input.read(buffer)) > 0) {
            System.out.println();
            zip.write(buffer, 0 , len);
        }
        zip.closeEntry();
        zip.flush();
        zip.close();
        input.close();
        filename += ".zip";
    } catch(IOException e) {
        e.printStackTrace();
    }
}

这些代码创建了一个zip文件,对我来说也是如此

简单的解决方案。在
ZipEntry
中创建一个不带文件分隔符的手动目录

zip.putNextEntry(new ZipEntry("LOG" + fileName));
而不是

zip.putNextEntry(new ZipEntry(fileName));
这里
fileName=file.getAbsoluteFile()


这将首先在zip文件中创建LOG dir,然后是带有目录路径的文件名。这避免了在zip文件中创建初始空目录。

您从不使用方法的
filename
参数,而是使用包含类中的全局filename字段。这可能不是解决方案,但它确实似乎是一个潜在的问题,而且不管怎样都有未使用的参数是不好的惯例。@我可以假设这只是一个糟糕的设计,看看他在这个方法中如何操作这个.filename,很可能是他在进入该方法之前设置的,完全忽略参数。@Vulcan我有一个版本使用文件名作为参数,但我更改了设计,忘记了删除它。
filename
this.filename
的用法非常奇怪。我打算写一个关于这个的答案,但我不想麻烦;)缺乏冲洗很可能不是问题所在;问题是该条目从未关闭,因此指针从未被确认写入过条目。@maksimov我有另一个版本使用了该文件名,但我决定改为使用this.filename,并忘了更新函数。很抱歉,今年夏天我做了三次实习。值得注意的是,如果发生IOException,所有这些close()调用也需要处理。我已经使用了你的建议,但是,zip文件仍然是empty@philippe这个文件名是什么?我注意到它有一个.zip附加到它来创建一个zip文件,这意味着它可能是一个目录。如果这是真的,你只是有逻辑上的缺陷,因为目录内容不能用那种方式写;相反,一旦您放置了一个目录条目,那么您就可以在该目录中为每个文件放置单独的条目,并按现在的方式写入这些条目。@Vulcan this.filename是该文件的名称,如果它不是zip文件的话。我只是在文件末尾添加.zip。到目前为止,它应该只压缩一个文件。对于它是一个目录的情况,我将更改算法以迭代该目录,在zip文件上创建假定的每个条目。正在生成的zip文件仍然为空:-(.这是因为zip条目的名称;它不能是绝对路径,它必须是相对路径。当我之前尝试相对路径时,我忘记删除前导反斜杠。这是输出:文件名:C:/Users/souzamor/workspace/xmlparser/src/PR_training.docx文件大小:306,因此创建的条目指向现有的file、 这就是我在zip.putNextEntry(new-ZipEntry(this.filename))中使用的;我明白了,我对你如何使用这个.namefile感到困惑,甚至没有使用namefile参数,所以我假设你错误地指向了一个不存在的文件。
final static byte[] EmptyZip={80,75,05,06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00};
public static void createEmptyZip(String path){
    try{
        FileOutputStream fos=new FileOutputStream(new File(path));
        fos.write(EmptyZip, 0, 22);
        fos.flush();
        fos.close();
    }catch (FileNotFoundException e){
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}