Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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/4/matlab/13.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_Directory_Fileinputstream_Zipoutputstream - Fatal编程技术网

Java 将文件添加到ZIP文件

Java 将文件添加到ZIP文件,java,zip,directory,fileinputstream,zipoutputstream,Java,Zip,Directory,Fileinputstream,Zipoutputstream,我试图将一些文件添加到ZIP文件中,它创建了该文件,但没有向其中添加任何内容。 代码1: 我的职能: public static void zipFolder(File folder, String name) throws Exception { byte[] buffer = new byte[18024]; ZipOutputStream out = new ZipOutputStream(new FileOutputStream(name)); FileInpu

我试图将一些文件添加到ZIP文件中,它创建了该文件,但没有向其中添加任何内容。 代码1:

我的职能:

public static void zipFolder(File folder, String name) throws Exception {
    byte[] buffer = new byte[18024];

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(name));
    FileInputStream in = new FileInputStream(folder);

    out.putNextEntry(new ZipEntry(name));

    int len;

    while((len = in.read(buffer)) > 0) {
        out.write(buffer, 0, len);
    }

    out.closeEntry();
    in.close();
    out.close();
}

编辑:我发现了问题,它只是在将文件从C:\drive写入F:\drive的ZIP文件时遇到了问题

您无法压缩文件夹,只能压缩文件。要压缩文件夹,必须手动添加所有子文件。我写了这个课程来做这个工作。您可以免费使用:)

用法如下:

List<File> sources = new ArrayList<File>();
sources.add(tobackup);
Packager.packZip(new File(zipName), sources);
List sources=new ArrayList();
来源。添加(tobackup);
packZip(新文件(zipName),源代码);
下面是课堂:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Packager
{
    public static void packZip(File output, List<File> sources) throws IOException
    {
        System.out.println("Packaging to " + output.getName());
        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(output));
        zipOut.setLevel(Deflater.DEFAULT_COMPRESSION);

        for (File source : sources)
        {
            if (source.isDirectory())
            {
                zipDir(zipOut, "", source);
            } else
            {
                zipFile(zipOut, "", source);
            }
        }
        zipOut.flush();
        zipOut.close();
        System.out.println("Done");
    }

    private static String buildPath(String path, String file)
    {
        if (path == null || path.isEmpty())
        {
            return file;
        } else
        {
            return path + "/" + file;
        }
    }

    private static void zipDir(ZipOutputStream zos, String path, File dir) throws IOException
    {
        if (!dir.canRead())
        {
            System.out.println("Cannot read " + dir.getCanonicalPath() + " (maybe because of permissions)");
            return;
        }

        File[] files = dir.listFiles();
        path = buildPath(path, dir.getName());
        System.out.println("Adding Directory " + path);

        for (File source : files)
        {
            if (source.isDirectory())
            {
                zipDir(zos, path, source);
            } else
            {
                zipFile(zos, path, source);
            }
        }

        System.out.println("Leaving Directory " + path);
    }

    private static void zipFile(ZipOutputStream zos, String path, File file) throws IOException
    {
        if (!file.canRead())
        {
            System.out.println("Cannot read " + file.getCanonicalPath() + " (maybe because of permissions)");
            return;
        }

        System.out.println("Compressing " + file.getName());
        zos.putNextEntry(new ZipEntry(buildPath(path, file.getName())));

        FileInputStream fis = new FileInputStream(file);

        byte[] buffer = new byte[4092];
        int byteCount = 0;
        while ((byteCount = fis.read(buffer)) != -1)
        {
            zos.write(buffer, 0, byteCount);
            System.out.print('.');
            System.out.flush();
        }
        System.out.println();

        fis.close();
        zos.closeEntry();
    }
}
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.util.List;
导入java.util.zip.Deflater;
导入java.util.zip.ZipEntry;
导入java.util.zip.ZipoutStream;
公营包装商
{
公共静态void packZip(文件输出,列表源)引发IOException
{
System.out.println(“打包到”+output.getName());
ZipOutputStream zipOut=newzipoutpstream(newfileoutputstream(output));
zipOut.setLevel(Deflater.DEFAULT_压缩);
用于(文件源:源)
{
if(source.isDirectory())
{
zipDir(zipOut,“,来源);
}否则
{
zipFile(zipOut,“”,来源);
}
}
齐平();
zipOut.close();
系统输出打印项次(“完成”);
}
私有静态字符串构建路径(字符串路径、字符串文件)
{
if(path==null | | path.isEmpty())
{
返回文件;
}否则
{
返回路径+“/”+文件;
}
}
私有静态void zipDir(ZipOutputStream zos、字符串路径、文件目录)引发IOException
{
如果(!dir.canRead())
{
System.out.println(“无法读取”+dir.getCanonicalPath()+“(可能是因为权限)”);
返回;
}
File[]files=dir.listFiles();
path=buildPath(path,dir.getName());
System.out.println(“添加目录”+路径);
用于(文件源:文件)
{
if(source.isDirectory())
{
zipDir(zos、路径、源);
}否则
{
zipFile(zos、路径、源);
}
}
System.out.println(“离开目录”+路径);
}
私有静态void zipFile(ZipOutputStream zos、字符串路径、文件文件)引发IOException
{
如果(!file.canRead())
{
System.out.println(“无法读取”+file.getCanonicalPath()+“(可能是因为权限问题)”);
返回;
}
System.out.println(“压缩”+file.getName());
putNextEntry(新的ZipEntry(buildPath(path,file.getName()));
FileInputStream fis=新的FileInputStream(文件);
字节[]缓冲区=新字节[4092];
int字节数=0;
while((字节计数=fis.read(缓冲区))!=-1)
{
写入(缓冲区,0,字节数);
系统输出打印('.');
System.out.flush();
}
System.out.println();
fis.close();
zos.closeEntry();
}
}
享受吧

编辑:要检查程序是否仍然忙,可以添加我用(*)标记的三行


编辑2:尝试新代码。在我的平台上,它运行正常(OSX)。我不确定,但在AppData中,Windows中的文件可能有一些有限的读取权限。

您不能压缩文件夹,只能压缩文件。要压缩文件夹,必须手动添加所有子文件。我写了这个课程来做这个工作。您可以免费使用:)

用法如下:

List<File> sources = new ArrayList<File>();
sources.add(tobackup);
Packager.packZip(new File(zipName), sources);
List sources=new ArrayList();
来源。添加(tobackup);
packZip(新文件(zipName),源代码);
下面是课堂:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Packager
{
    public static void packZip(File output, List<File> sources) throws IOException
    {
        System.out.println("Packaging to " + output.getName());
        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(output));
        zipOut.setLevel(Deflater.DEFAULT_COMPRESSION);

        for (File source : sources)
        {
            if (source.isDirectory())
            {
                zipDir(zipOut, "", source);
            } else
            {
                zipFile(zipOut, "", source);
            }
        }
        zipOut.flush();
        zipOut.close();
        System.out.println("Done");
    }

    private static String buildPath(String path, String file)
    {
        if (path == null || path.isEmpty())
        {
            return file;
        } else
        {
            return path + "/" + file;
        }
    }

    private static void zipDir(ZipOutputStream zos, String path, File dir) throws IOException
    {
        if (!dir.canRead())
        {
            System.out.println("Cannot read " + dir.getCanonicalPath() + " (maybe because of permissions)");
            return;
        }

        File[] files = dir.listFiles();
        path = buildPath(path, dir.getName());
        System.out.println("Adding Directory " + path);

        for (File source : files)
        {
            if (source.isDirectory())
            {
                zipDir(zos, path, source);
            } else
            {
                zipFile(zos, path, source);
            }
        }

        System.out.println("Leaving Directory " + path);
    }

    private static void zipFile(ZipOutputStream zos, String path, File file) throws IOException
    {
        if (!file.canRead())
        {
            System.out.println("Cannot read " + file.getCanonicalPath() + " (maybe because of permissions)");
            return;
        }

        System.out.println("Compressing " + file.getName());
        zos.putNextEntry(new ZipEntry(buildPath(path, file.getName())));

        FileInputStream fis = new FileInputStream(file);

        byte[] buffer = new byte[4092];
        int byteCount = 0;
        while ((byteCount = fis.read(buffer)) != -1)
        {
            zos.write(buffer, 0, byteCount);
            System.out.print('.');
            System.out.flush();
        }
        System.out.println();

        fis.close();
        zos.closeEntry();
    }
}
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.util.List;
导入java.util.zip.Deflater;
导入java.util.zip.ZipEntry;
导入java.util.zip.ZipoutStream;
公营包装商
{
公共静态void packZip(文件输出,列表源)引发IOException
{
System.out.println(“打包到”+output.getName());
ZipOutputStream zipOut=newzipoutpstream(newfileoutputstream(output));
zipOut.setLevel(Deflater.DEFAULT_压缩);
用于(文件源:源)
{
if(source.isDirectory())
{
zipDir(zipOut,“,来源);
}否则
{
zipFile(zipOut,“”,来源);
}
}
齐平();
zipOut.close();
系统输出打印项次(“完成”);
}
私有静态字符串构建路径(字符串路径、字符串文件)
{
if(path==null | | path.isEmpty())
{
返回文件;
}否则
{
返回路径+“/”+文件;
}
}
私有静态void zipDir(ZipOutputStream zos、字符串路径、文件目录)引发IOException
{
如果(!dir.canRead())
{
System.out.println(“无法读取”+dir.getCanonicalPath()+“(可能是因为权限)”);
返回;
}
File[]files=dir.listFiles();
path=buildPath(path,dir.getName());
System.out.println(“添加目录”+路径);
用于(文件源:文件)
{
if(source.isDirectory())
{
zipDir(zos、路径、源);
}否则
{
zipFile(zos、路径、源);
}
}
System.out.println(“离开目录”+路径);
}
私有静态void zipFile(ZipOutputStream zos、字符串路径、文件文件)引发IOException
{
如果(!file.canRead())
{
System.out.println(“无法读取”+文件.getCanonicalPath()+”