Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.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 ZipEntry和Zipoutputstream目录_Java_Zip - Fatal编程技术网

Java ZipEntry和Zipoutputstream目录

Java ZipEntry和Zipoutputstream目录,java,zip,Java,Zip,我有这段代码 public void doBuild() throws IOException { ZipEntry sourceEntry=new ZipEntry(sourcePath); ZipEntry assetEntry=new ZipEntry(assetPath); ZipOutputStream out = new ZipOutputStream(new FileOutputStream("output/"+workOn.getName().rep

我有这段代码

   public void doBuild() throws IOException {
    ZipEntry sourceEntry=new ZipEntry(sourcePath);
    ZipEntry assetEntry=new ZipEntry(assetPath);
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream("output/"+workOn.getName().replaceAll(".bld"," ")+buildNR+".zip"));
     out.putNextEntry(sourceEntry);
    out.putNextEntry(assetEntry);
    out.close();
    System.err.println("Build success!");
    increaseBuild();

}
因此,如果我运行它,它会很好地运行,创建.zip和所有文件,但是zip文件是空的。sourceEntry和assetEntry都是目录。我怎样才能轻松地将这些目录放到我的.zip中


对于那些感兴趣的人来说,这是一个MC-mod构建系统,可以在上找到。注意:上面的代码还没有提交或推送到那里

试试这样的方法。参数useFullFileNames指定 是否要保留指向的路径的全名 要压缩的文件

如果你有两个文件 /dir1/dir2/a.txt/dir1/b.txt UseFullFileName指定您是否希望最终在中查看 zip文件包含两个文件的原始路径,或者只是 这两个文件没有这样的路径 a.txtb.txt 在您创建的zip文件的根目录中

注意,在我的示例中,压缩的文件 实际上是先读取,然后再写入输出。 我想你错过了那部分

    public static boolean createZip(String fNameZip, boolean useFullFileNames, String... fNames) throws Exception {
        try {
            int cntBufferSize = 256 * 1024;
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(fNameZip);
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
            byte bBuffer[] = new byte[cntBufferSize];
            File ftmp = null;
            for (int i = 0; i < fNames.length; i++) {
                if (fNames[i] != null) {
                    FileInputStream fi = new FileInputStream(fNames[i]);
                    origin = new BufferedInputStream(fi, cntBufferSize);
                    ftmp = new File(fNames[i]);
                    ZipEntry entry = new ZipEntry(useFullFileNames ? fNames[i] : ftmp.getName());
                    out.putNextEntry(entry);
                    int count;
                    while ((count = origin.read(bBuffer, 0, cntBufferSize)) != -1) {
                        out.write(bBuffer, 0, count);
                    }
                    origin.close();
                }
            }
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
public static boolean createZip(String fNameZip、boolean useFullFileNames、String…fNames)引发异常{
试一试{
int cntBufferSize=256*1024;
BufferedInputStream原点=null;
FileOutputStream dest=新的FileOutputStream(fNameZip);
ZipOutputStream out=新的zipoutpstream(新的缓冲输出流(dest));
字节bBuffer[]=新字节[cntBufferSize];
文件ftmp=null;
for(int i=0;i
列出两个目录中的文件名,合并两个列表,然后使用我提供的方法/函数将此联合列表作为参数fNames的值传递。这应该行得通,我用过,它发出声音。。浏览器显示zip文件为空,但7zip不显示内容