-
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();
- }
- }
-
相关阅读:
MySql 应用语句
MySql 存储过程 退出
MySql 存储过程 光标只循环一次
MySql获取两个日期间的时间差
VM VirtualBox 全屏模式 && 自动缩放模式 相互切换
区分不同操作系统、编译器不同版本的宏
debian下配置网络 安装无线网卡驱动 Broadcom BCMXX系列
YII 主题设置
Unix线程概念、控制原语、属性
远程IPC种植木马
-
原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/3871114.html
Copyright © 2020-2023
润新知