package cn.cnnho.backstage.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.*;
import java.io.*;
public class Test {
/**
* 压缩一个文件到一个zip
* @param in 输入的文件
* @param out 输出的zip名
* @param outEntry zip中的条目名
*/
public static void zip(String in,ZipOutputStream out,String outEntry) {
File inf = new File(in) ;
FileInputStream ins;
try {
if(inf.exists()){
ins = new FileInputStream(inf);

//文件夹根据需求动态填充
out.putNextEntry(new ZipEntry("文件夹"+"/"+outEntry));
int b ;
while ((b = ins.read()) != -1) {
out.write(b);
}
ins.close();
}else{
//按照项目的需要在没有的时候给予提示吧。
System.out.println("系统找不到指定文件:"+in);
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
} catch (Exception e3) {
e3.printStackTrace();
}
}

/**
*
* @param out 输出的zip名
* @param in 输入的目录
* @param inEntrys 输入的条目集合
*/
public static void zip(String out,String in,List<String> inEntrys) {
try {
OutputStream zipFileName_fileOutputStream = new FileOutputStream(out);
ZipOutputStream zipOutputStream = new ZipOutputStream(zipFileName_fileOutputStream);
for(String inEntry:inEntrys){
zip(in+inEntry,zipOutputStream,inEntry);
}
zipOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {

List<String> inEntrys=new ArrayList<String>();
inEntrys.add("a.txt");
inEntrys.add("b.txt");
//可以加一个实际没有的文件试试
inEntrys.add("c.txt");

zip("d:\\a\\a3.zip","d:\\a\\",inEntrys);

}
}

java使用ZipOutputStream批量压缩文件,并将文件分别放置不同文件夹压缩的更多相关文章

  1. java/resteasy批量下载存储在阿里云OSS上的文件,并打包压缩

    现在需要从oss上面批量下载文件并压缩打包,搜了很多相关博客,均是缺胳膊少腿,要么是和官网说法不一,要么就压缩包工具类不给出 官方API https://help.aliyun.com/documen ...

  2. 使用Java API进行tar.gz文件及文件夹压缩解压缩

    在java(JDK)中我们可以使用ZipOutputStream去创建zip压缩文件,(参考我之前写的文章 使用java API进行zip递归压缩文件夹以及解压 ),也可以使用GZIPOutputSt ...

  3. Java实现快速批量移动文件

    文件移动是计算机资源管理常用的一个操作,这在操作系统中可以通过文件的剪切与复制或鼠标拖动来实现.但是在Java文件的编程实现中,大多是通过复制文件到目的地,再删除所有文件来实现的.这对于小文件来说看不 ...

  4. C#文件或文件夹压缩和解压

    C#文件或文件夹压缩和解压方法有很多,本文通过使用ICSharpCode.SharpZipLib.dll来进行压缩解压 1.新建一个winform项目,选择项目右键 管理NuGet程序包,搜索ICSh ...

  5. SharpZipLib 文件/文件夹压缩

    一.ZipFile ZipFile类用于选择文件或文件夹进行压缩生成压缩包. 常用属性: 属性 说明 Count 文件数目(注意是在ComitUpdat之后才有) Password 压缩包密码 Siz ...

  6. C# 文件/文件夹压缩

    一.ZipFile ZipFile类用于选择文件或文件夹进行压缩生成压缩包. 常用属性: 属性 说明 Count 文件数目(注意是在ComitUpdat之后才有) Password 压缩包密码 Siz ...

  7. C# 文件/文件夹压缩解压缩

    项目上用到的,随手做个记录,哈哈. 直接上代码: using System; using System.Data; using System.Configuration; using System.C ...

  8. 项目一:第四天 1、快递员的条件分页查询-noSession,条件查询 2、快递员删除(逻辑删除) 3、基于Apache POI实现批量导入区域数据 a)Jquery OCUpload上传文件插件使用 b)Apache POI读取excel文件数据

    1. 快递员的条件分页查询-noSession,条件查询 2. 快递员删除(逻辑删除) 3. 基于Apache POI实现批量导入区域数据 a) Jquery OCUpload上传文件插件使用 b) ...

  9. springMVC实现基本文件夹压缩下载功能

    将文件夹压缩后下载: @Slf4j public class Test { private static final String BASE_PATH = "/root/doc/" ...

随机推荐

  1. sublime text 常用插件安装

    一.安装方法 ctrl+ship+p —— 在弹出的地方输入 pci (package control: install pagckage)—— 再输入 要安装的包名 二.一步过慢或失败解决: 原因: ...

  2. junit基础学习之-简介(1)

    JUnit介绍 JUnit是一个开源的Java单元测试框架,由 Erich Gamma 和 Kent Beck 开发完成. 1  JUnit简介 JUnit主要用来帮助开发人员进行Java的单元测试, ...

  3. Linux重要命令练习之bc

  4. datetime使用

      通过当前日期,获取最近第一个周五.第二个周五.每季度末最后一个周五 def get_current_week(self, symbol: str, start_date: datetime): i ...

  5. 在Mac上使用docker+sql server+Navicat

    1. 版本:  2. 安装Kubernetes(并不知道安装这个有什么用) git clone https://github.com/maguowei/k8s-docker-desktop-for-m ...

  6. 学术Essay写作中Introduction的正确打开方式

    其实在学术essay写作过程中,很多留学生经常不知道如何写introduction,所以有些开头的模板句就出现了,比如,With the development of society/With the ...

  7. 吴裕雄--天生自然TensorFlow2教程:张量限幅

    import tensorflow as tf a = tf.range(10) a # a中小于2的元素值为2 tf.maximum(a, 2) # a中大于8的元素值为8 tf.minimum(a ...

  8. C#构造函数调用其他构造函数

    http://blog.csdn.net/dogfish/article/details/6990266  <-- 虏来的地 public class Class1 { public Class ...

  9. python----linux下简单的排序

    1.选择排序:把一个数与余下所有的数排序,最小的排到最前面 [root@besttest liyn_test]# cat test.py #! /usr/bin/python a=[,,,] ,len ...

  10. 根据M3U8地址下载视频

     下面展示的是通过M3U8地址,把这个地址转为一个视频文件:只是一个基本案例,当然,有些下载的M3U8文件里面格式是不一样的,还有的是加过密的,道理都是一个道理. import java.io.Buf ...