Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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 byte[]buf=新字节[1024]//是时候制作zip文件了 字符串zipName=“name.zip”; ZipOutputStream out=newzipoutpstream(newfileoutputstream(zipName)); 对于(int i=0;i0){ out.write(buf,0,len); } out.closeEntry(); zipFile.close(); } out.close();_Java_File_Zip - Fatal编程技术网

java,将非本地文件放入zip byte[]buf=新字节[1024]//是时候制作zip文件了 字符串zipName=“name.zip”; ZipOutputStream out=newzipoutpstream(newfileoutputstream(zipName)); 对于(int i=0;i0){ out.write(buf,0,len); } out.closeEntry(); zipFile.close(); } out.close();

java,将非本地文件放入zip byte[]buf=新字节[1024]//是时候制作zip文件了 字符串zipName=“name.zip”; ZipOutputStream out=newzipoutpstream(newfileoutputstream(zipName)); 对于(int i=0;i0){ out.write(buf,0,len); } out.closeEntry(); zipFile.close(); } out.close();,java,file,zip,Java,File,Zip,copy是一个字符串数组,其中包含我想要的每个文件的文件路径(例如C:\files\test.pdf)。它编译和运行正常,没有异常,并创建了zip文件夹,但所述zip文件夹中没有任何内容。修复了它,如果其他人需要它,请发布答案 byte[] buf = new byte[1024];//time to make zip file String zipName="name.zip"; ZipOutputStream out = new ZipOutputStream(new Fil

copy是一个字符串数组,其中包含我想要的每个文件的文件路径(例如C:\files\test.pdf)。它编译和运行正常,没有异常,并创建了zip文件夹,但所述zip文件夹中没有任何内容。

修复了它,如果其他人需要它,请发布答案

byte[] buf = new byte[1024];//time to make zip file
    String zipName="name.zip";
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipName));

    for(int i=0; i<copy.length; i++){//put all pdfs in the zip
        FileInputStream zipFile = new FileInputStream(copy[i]);
        out.putNextEntry(new ZipEntry(copy[i]));
        int len;
        while((len=zipFile.read(buf))>0){
            out.write(buf, 0, len);             
        }
        out.closeEntry();
        zipFile.close();
    }   
out.close();

文件是一个文件数组(我用来创建副本)。

修复了它,如果其他人需要它,就发布答案

byte[] buf = new byte[1024];//time to make zip file
    String zipName="name.zip";
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipName));

    for(int i=0; i<copy.length; i++){//put all pdfs in the zip
        FileInputStream zipFile = new FileInputStream(copy[i]);
        out.putNextEntry(new ZipEntry(copy[i]));
        int len;
        while((len=zipFile.read(buf))>0){
            out.write(buf, 0, len);             
        }
        out.closeEntry();
        zipFile.close();
    }   
out.close();

files是一个文件数组(我用来创建副本)。

File[]files=myFile.listFiles();复制[i]=文件[i].toString();在一个循环中。我把它打印出来了,而且都是有效的。如果你把文件放在
文件
的数组中,为什么要把它们转换成
字符串
的数组?支持直接从
文件
对象打开
文件输入流
,并且更好地面向对象。另外,如果检查循环中的名称,为什么不同时检查
System.out.println(“文件(“+files[i].toString()+”)exists()=”+files[i].exists())您可能在不同的路径上误译了。我转换为字符串是因为out.putnextery(新的ZipEntry(copy[I]);只接受一个字符串。我已经打印出了两个文件夹的内容,并进行了存在检查。File[]files=myFile.listFiles();复制[i]=文件[i].toString();在一个循环中。我把它打印出来了,而且都是有效的。如果你把文件放在
文件
的数组中,为什么要把它们转换成
字符串
的数组?支持直接从
文件
对象打开
文件输入流
,并且更好地面向对象。另外,如果检查循环中的名称,为什么不同时检查
System.out.println(“文件(“+files[i].toString()+”)exists()=”+files[i].exists())您可能在不同的路径上误译了。我转换为字符串是因为out.putnextery(新的ZipEntry(copy[I]);只接受一个字符串。我已经打印了这两个文件夹的内容,并且进行了存在检查。祝贺你找到了答案。文档中没有提到不使用路径(尽管考虑zip中的条目是什么是有意义的)。你应该接受你的答案,但是你不能在一天内接受你自己的答案。谢谢你。恭喜你弄明白了。文档中没有提到不使用路径(尽管考虑zip中的条目是什么是有意义的)。你应该接受你的答案,但是你不能在一天内接受你自己的答案。谢谢你。