将文件添加到 ZIP 文件 [英] Adding files to ZIP file

查看:24
本文介绍了将文件添加到 ZIP 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些文件添加到 ZIP 文件中,它会创建该文件但并未向其中添加任何内容.代码 1:

I am trying to add some files to a ZIP file, it creates the file but does not add anything into it. Code 1:

String fulldate = year + "-" + month + "-" + day + "-" + min;

File dateFolder = new File("F:\" + compname + "\" + fulldate);
dateFolder.mkdir();

String zipName = "F:\" + compname + "\" + fulldate + "\" + fulldate + ".zip";

zipFolder(tobackup, zipName);

我的功能:

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: 驱动器中的文件写入 F: 驱动器中的 ZIP 时遇到问题

I found the problem, it was just having trouble writing files from the C: drive into a ZIP in the F: drive

推荐答案

您不能压缩文件夹,只能压缩文件.要压缩文件夹,您必须手动添加所有子文件.我写了这门课来完成这项工作.你可以免费拥有它:)

You can't zip folders, only files. To zip folders, you have to add all the subfiles manually. I wrote this class that does the job. You can have it for free :)

用法是这样的:

List<File> sources = new ArrayList<File>();
sources.add(tobackup);
Packager.packZip(new File(zipName), sources);

这是课程:

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();
    }
}

享受吧!

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

EDIT: To check if the program is still busy, you can add the three lines I marked with a (*)

EDIT 2:尝试新代码.在我的平台上,它运行正确(OS X).我不确定,但 AppData 中 Windows 文件的读取权限可能有限.

EDIT 2: Try the new code. On my platform, it runs correct (OS X). I'm not sure but, there might be some limited read permissions for files in Windows in AppData.

这篇关于将文件添加到 ZIP 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆