Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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_File_Zip_Fileinputstream_Zipoutputstream - Fatal编程技术网

Java 如何创建多个图像文件的zip文件

Java 如何创建多个图像文件的zip文件,java,file,zip,fileinputstream,zipoutputstream,Java,File,Zip,Fileinputstream,Zipoutputstream,我正在尝试创建一个包含多个图像文件的zip文件。我已经成功地创建了所有图像的zip文件,但不知何故,所有图像都被挂起到950字节。我不知道这里出了什么问题,现在我无法打开压缩到zip文件中的图像 这是我的密码。有人能告诉我这是怎么回事吗 String path="c:\\windows\\twain32"; File f=new File(path); f.mkdir(); File x=new File("e:\\test"); x.mkdir(); byte []b; String zipF

我正在尝试创建一个包含多个图像文件的zip文件。我已经成功地创建了所有图像的zip文件,但不知何故,所有图像都被挂起到950字节。我不知道这里出了什么问题,现在我无法打开压缩到zip文件中的图像

这是我的密码。有人能告诉我这是怎么回事吗

String path="c:\\windows\\twain32";
File f=new File(path);
f.mkdir();
File x=new File("e:\\test");
x.mkdir();
byte []b;
String zipFile="e:\\test\\test.zip";
FileOutputStream fout=new FileOutputStream(zipFile);
ZipOutputStream zout=new ZipOutputStream(new BufferedOutputStream(fout));


File []s=f.listFiles();
for(int i=0;i<s.length;i++)
{
    b=new byte[(int)s[i].length()];
    FileInputStream fin=new FileInputStream(s[i]);
    zout.putNextEntry(new ZipEntry(s[i].getName()));
    int length;
    while((length=fin.read())>0)
    {
        zout.write(b,0,length);
    }
    zout.closeEntry();
    fin.close();
}
zout.close();
String path=“c:\\windows\\twain32”;
文件f=新文件(路径);
f、 mkdir();
文件x=新文件(“e:\\test”);
x、 mkdir();
字节[]b;
String zipFile=“e:\\test\\test.zip”;
FileOutputStream fout=新的FileOutputStream(zipFile);
ZipOutputStream zout=新的zipoutpstream(新的缓冲输出流(fout));
文件[]s=f.listFiles();
对于(int i=0;i0)
{
写入(b,0,长度);
}
zout.closeEntry();
fin.close();
}
zout.close();
更改此选项:

while((length=fin.read())>0)
为此:

while((length=fin.read(b, 0, 1024))>0)
并将缓冲区大小设置为1024字节:

b=new byte[1024];

这是我的zip函数,我总是用于任何文件结构:

public static File zip(List<File> files, String filename) {
    File zipfile = new File(filename);
    // Create a buffer for reading the files
    byte[] buf = new byte[1024];
    try {
        // create the ZIP file
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
        // compress the files
        for(int i=0; i<files.size(); i++) {
            FileInputStream in = new FileInputStream(files.get(i).getCanonicalName());
            // add ZIP entry to output stream
            out.putNextEntry(new ZipEntry(files.get(i).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();
        }
        // complete the ZIP file
        out.close();
        return zipfile;
    } catch (IOException ex) {
        System.err.println(ex.getMessage());
    }
    return null;
}
公共静态文件zip(列表文件、字符串文件名){
File zipfile=新文件(文件名);
//创建用于读取文件的缓冲区
字节[]buf=新字节[1024];
试一试{
//创建ZIP文件
ZipOutputStream out=newzipoutpstream(newfileoutputstream(zipfile));
//压缩文件
对于(int i=0;i 0){
out.write(buf,0,len);
}
//完成条目
out.closeEntry();
in.close();
}
//完成ZIP文件
out.close();
返回zipfile;
}捕获(IOEX异常){
System.err.println(例如getMessage());
}
返回null;
}

谢谢它工作得很好你已经解决了问题谢谢兄弟非常感谢….:如果你认为这个答案合适,接受它。同样的道理也适用于你在过去问过的所有问题,感谢这个示例很好地工作,除了我必须更改这一行:FileInputStream in=newfileinputstream(files.get(I).getCanonicalName());您好-您说得对,为了保持文件夹结构,最好使用*.getCanonicalName(),我已经根据我的答案对其进行了调整-谢谢。您好。我想知道为什么我们在out.write语句之后不清除缓冲区,缓冲区不会溢出吗?(ii)对于大小小于1 MB的文件,通常应保持什么样的缓冲区大小?