Java未写入zip文件
我尝试使用以下java方法创建一个zip文件,其中写入了一个现有文件(zip文件的名称相同,只是扩展名.log替换为.zip)。zip文件创建成功,但完成后文件不在其中 这是我的密码:Java未写入zip文件,java,zip,zipfile,Java,Zip,Zipfile,我尝试使用以下java方法创建一个zip文件,其中写入了一个现有文件(zip文件的名称相同,只是扩展名.log替换为.zip)。zip文件创建成功,但完成后文件不在其中 这是我的密码: private static void zipFile(File fileToZip) { final int bufferSize = 2048; File zipFile = new File(fileToZip.getAbsolutePath().replaceAll(".log", ".
private static void zipFile(File fileToZip) {
final int bufferSize = 2048;
File zipFile = new File(fileToZip.getAbsolutePath().replaceAll(".log", ".zip"));
try (FileOutputStream fos = new FileOutputStream(zipFile.getAbsolutePath());
FileInputStream fis = new FileInputStream(fileToZip);
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
BufferedInputStream origin = new BufferedInputStream(fis, bufferSize)) {
ZipEntry ze = new ZipEntry(fileToZip.getAbsolutePath());
zos.putNextEntry(ze);
byte[] data = new byte[bufferSize];
int count;
while ((count = origin.read(data, 0, bufferSize)) != -1) {
LOGGER.info("WRITING!!!");
zos.write(data, 0, count);
}
zos.closeEntry();
} catch (IOException e) {
LOGGER.error("Error: ", e);
}
}
有什么想法吗?:) 变化
ZipEntry ze = new ZipEntry(fileToZip.getAbsolutePath());
到
改变
到
虽然使用绝对路径名是一个奇怪的选择,但对我有效。虽然使用绝对路径名是一个奇怪的选择,但对我有效。
ZipEntry ze = new ZipEntry(fileToZip.getName());