java读取/生成ZIP

public class ZipUtils {

	public static void downloadFiles(String srcSource, String pin, String shmy, String url) {
		HttpServletResponse response = HttpContextUtils.getHttpServletResponse();
		String downloadName = "gateway.zip";
		// 将文件进行打包下载
		try {
			OutputStream out = response.getOutputStream();
			byte[] data = createZip(srcSource, pin, shmy, url);// 服务器存储地址
			response.reset();
			response.setHeader("Content-Disposition", "attachment;fileName=" + downloadName);
			response.addHeader("Content-Length", "" + data.length);
			response.setContentType("application/octet-stream;charset=UTF-8");
			IOUtils.write(data, out);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static byte[] createZip(String srcSource, String pin, String shmy, String url) throws Exception {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		// 将目标文件打包成zip导出
		File file = new File(srcSource);
		compressZip(zip, file, "");
		appendPropFile(zip, "/prop.properties", pin, shmy, url);
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

	public static void compressZip(ZipOutputStream zip, File file, String dir) throws Exception {
		// 如果当前的是文件夹,则进行进一步处理
		if (file.isDirectory()) {
			// 得到文件列表信息
			File[] files = file.listFiles();
			// 将文件夹添加到下一级打包目录
			zip.putNextEntry(new ZipEntry(dir + "/"));
			dir = dir.length() == 0 ? "" : dir + "/";
			// 循环将文件夹中的文件打包
			for (int i = 0; i < files.length; i++) {
				compressZip(zip, files[i], dir + files[i].getName()); // 递归处理
			}
		} else { // 当前的是文件,打包处理
			// 文件输入流
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
			ZipEntry entry = new ZipEntry(dir);
			zip.putNextEntry(entry);
			zip.write(FileUtils.readFileToByteArray(file));
			IOUtils.closeQuietly(bis);
			zip.flush();
			zip.closeEntry();
		}
	}

	public static void appendPropFile(ZipOutputStream zip, String dir, String pin, String shmy, String url)
			throws Exception {
		String str = "[sys]\r\ncipher=" + shmy + "\r\nport=6888\r\nurl=" + url + "\r\npin=" + pin;
		ZipEntry entry = new ZipEntry(dir);
		zip.putNextEntry(entry);
		zip.write(str.getBytes("UTF-8"));
		zip.flush();
		zip.closeEntry();
	}
	
	public static void main11(String[] args) throws IOException {
        String path = "C:\\Users\\Administrator\\Desktop\\201904281023332820200407.zip";
        InputStream in = new BufferedInputStream(new FileInputStream(path));
        Charset gbk = Charset.forName("gbk");
        ZipInputStream zin = new ZipInputStream(in,gbk);
        ZipEntry ze;
        while((ze = zin.getNextEntry()) != null){
            if(ze.toString().endsWith("txt")){
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(zin));
                String line;
                while((line = br.readLine()) != null){
                    System.out.println(line.toString());
                }
            }
            System.out.println();
        }
        zin.closeEntry();
        in.close();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值