NutzCN Logo
问答 想通过不断调用压缩方法,将多个文件压缩在一起,结果只保留最后一个
发布于 2693天前 作者 efraiser 1925 次浏览 复制 上一个帖子 下一个帖子
标签:

//我写的方法如下,但是调用之后之前压缩的文件不在了,压缩文件里只是保留最后一个要压缩的文件,求大神解答
public boolean fileToZip(StringBuffer filePath, String zipFilePath, String fileName) {
System.out.println(">>>>>>开始压缩文件>>>文件路径" + filePath.toString() + ">>>压缩路径>>>" + zipFilePath + ">>>压缩文件名称:" + fileName + "<<<<<<");
boolean flag = false;
File sourceFile = new File(filePath.toString());// 创建文件
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
if (sourceFile.exists() == false) {
System.out.println(">>>>>> 待压缩的文件不存在:" + filePath.toString() + " 不存在. <<<<<<");
} else {
try {
File zipFile = new File(zipFilePath + "/" + fileName + ".zip");// 创建压缩文件
if (!zipFile.exists()) {
System.out.println("压缩文件不存在,创建压缩文件!!!");
zipFile.getParentFile().mkdirs();
zipFile.createNewFile();
}
fos = new FileOutputStream(zipFile,true);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs = new byte[1024 * 10];
// 创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(sourceFile.getName());
zos.putNextEntry(zipEntry);
// 读取待压缩的文件并写进压缩包里
fis = new FileInputStream(sourceFile);
bis = new BufferedInputStream(fis, 1024 * 10);
int read = 0;
while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
zos.write(bufs, 0, read);
}
if (null != fis)
fis.close();
// sourceFiles[i].delete();
flag = true;

       } catch (FileNotFoundException e) {
         e.printStackTrace();
         throw new RuntimeException(e);
       } catch (IOException e) {
         e.printStackTrace();
         throw new RuntimeException(e);
       } finally {
         // 关闭流
         try {
          if (null != bis)
              bis.close();
          if (null != zos)
              zos.close();
         } catch (IOException e) {
          e.printStackTrace();
          throw new RuntimeException(e);
         }
       }
    }
    return flag;
}
9 回复

这代码每次都是新建一个ZipFile

用添加N个文件的话

 zos = new ZipOutputStream(new BufferedOutputStream(fos));
for (xxx xx : list) {
       zipEntry = new ZipEntry(...);
	   zos.putNextEntry(zipEntry);
	   ...
}
zos.close();

@wendal 我在代码中不是加了判断了吗?这样没有作用?
File zipFile = new File(zipFilePath + "/" + fileName + ".zip");// 创建压缩文件
if (!zipFile.exists()) {
System.out.println("压缩文件不存在,创建压缩文件!!!");
zipFile.getParentFile().mkdirs();
zipFile.createNewFile();
}

@wendal 这个方式我之前不适用我的情况,我需要将文件添加到已经存在的文件夹中

@qq_11f99046 创建123.zip然后abc.zip消失了的意思吗?

@wendal 是我123.zip里面本来有一个1.xml文件,我想将2.xml也添加到123.zip这个压缩包中。执行我写的方法后123.zip中只有2.xml文件,1.xml文件不见了

@wendal 是我123.zip里面本来有一个1.xml文件,我想将2.xml也添加到123.zip这个压缩包中。执行我写的方法后123.zip中只有2.xml文件,1.xml文件不见了

@wendal
链接里面的方法
Map<String, String> env = new HashMap<>();
env.put("create", "true");
Path path = Paths.get("test.zip");
URI uri = URI.create("jar:" + path.toUri());
try (FileSystem fs = FileSystems.newFileSystem(uri, env))
{
Path nf = fs.getPath("new.txt");
try (Writer writer = Files.newBufferedWriter(nf, StandardCharsets.UTF_8, StandardOpenOption.CREATE)) {
writer.write("hello");
}
}

这个看不懂什么意思。。。

添加回复
请先登陆
回到顶部