我想添加一些文件到一个ZIP文件,它创建的文件,但不添加任何东西进去。 代码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:\驱动器
你不能压缩文件夹中的文件。 要压缩文件夹,您必须手动添加所有子文件。 我写了这个类,没有工作。 你可以把它免费:)
用法是这样的:
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();
}
}
请享用!
编辑 :要检查程序仍然忙,你可以添加三行我标有(*)
编辑2:尝试新的代码。 在我的平台,它运行正确(OS X)。 我不知道,但有可能是文件的一些有限的读取权限在Windows的应用程序数据。
又见ZeroTurnaround的邮编库 。 它具有这样的特征(引用):