-
java压缩解压zip文件,中文乱码还需要ant.jar包
va] view plaincopyprint?
- package cn.cn;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Enumeration;
- import java.util.zip.Deflater;
-
- import org.apache.tools.zip.ZipEntry;
- import org.apache.tools.zip.ZipFile;
- import org.apache.tools.zip.ZipOutputStream;
-
- public class ZipUtil {
- private ZipFile zipFile;
- private ZipOutputStream zipOut;
- private int bufSize;
- private byte[] buf;
-
- public ZipUtil(){
-
- this.bufSize = 1024*4;
- this.buf = new byte[this.bufSize];
- }
-
-
- public void doZip(String srcFile, String destFile) {
- File zipFile = new File(srcFile);
- try {
-
- this.zipOut = new ZipOutputStream(new BufferedOutputStream(
- new FileOutputStream(destFile)));
-
- zipOut.setComment("comment");
-
- zipOut.setEncoding("GBK");
-
- zipOut.setMethod(ZipOutputStream.DEFLATED);
-
-
- zipOut.setLevel(Deflater.BEST_COMPRESSION);
-
- handleFile(zipFile, this.zipOut,"");
-
- this.zipOut.close();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
-
-
- private void handleFile(File zipFile, ZipOutputStream zipOut,String dirName) throws IOException {
- System.out.println("遍历文件:"+zipFile.getName());
-
- if(zipFile.isDirectory())
- {
- File[] files = zipFile.listFiles();
-
- if (files.length == 0) {
-
- this.zipOut.putNextEntry(new ZipEntry(dirName+zipFile.getName()+File.separator));
- this.zipOut.closeEntry();
- } else {
- for (File file : files) {
-
- handleFile(file, zipOut, dirName+zipFile.getName()+File.separator);
- }
- }
- }
-
- else
- {
- FileInputStream fileIn = new FileInputStream(zipFile);
-
- this.zipOut.putNextEntry(new ZipEntry(dirName+zipFile.getName()));
- int length = 0;
-
- while ((length = fileIn.read(this.buf)) > 0) {
- this.zipOut.write(this.buf, 0, length);
- }
-
- this.zipOut.closeEntry();
- }
-
- }
-
-
- public void unZip(String unZipfile, String destFile) {
- FileOutputStream fileOut;
- File file;
- InputStream inputStream;
-
- try {
-
- this.zipFile = new ZipFile(unZipfile);
-
- for (@SuppressWarnings("unchecked")
- Enumeration<ZipEntry> entries = this.zipFile.getEntries(); entries
- .hasMoreElements();) {
- ZipEntry entry = entries.nextElement();
-
- file = new File(destFile+File.separator+entry.getName());
-
- if (entry.isDirectory()) {
- file.mkdirs();
- } else {
-
- File parent = file.getParentFile();
- if (!parent.exists()) {
- parent.mkdirs();
- }
-
- inputStream = zipFile.getInputStream(entry);
-
- fileOut = new FileOutputStream(file);
- int length = 0;
-
- while ((length = inputStream.read(this.buf)) > 0) {
- fileOut.write(this.buf, 0, length);
- }
- fileOut.close();
- inputStream.close();
- }
- }
- this.zipFile.close();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
-
相关阅读:
centos7安装jdk8
centos7安装mysql5.7.31
docker打包,运行springboot
centos7安装docker
ps学习记录
Html的学习(二)
tensorflow C++接口调用图像分类pb模型代码
tensorflow C++接口调用目标检测pb模型代码
ubuntu14 编译tensorflow C++ 接口
Python opencv计算批量图片的BGR各自的均值
-
原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/3871114.html
Copyright © 2020-2023
润新知