java zip文件操作,java 关于 zip 文件 的 基本操作
这里用的是appache的包 为了防止文件中文名乱码问题
java 操作 .zip 文件 压缩文件 解压文件 删除文件夹等
package com.gaeainfo.toponymbase.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.List;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
public class ZipUtil {
private static MessageManager msgManager1 = new MessageManager(
"bundle.toponymbase");
private String comment = "";
public void setComment(String comment) {
this.comment = comment;
}
/**
* @param src:要压缩的目录
* @param dest:压缩文件存档
* @throws Exception
*/
public void zip(String src, String dest, List filter) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(dest));
File srcFile = new File(src);
zip(out,srcFile,"",filter);
out.close();
}
/**
* @param out
* @param srcFile
* @param base:根路径
* @param filter:过滤
* @throws Exception
*/
public void zip(ZipOutputStream out, File srcFile, String base, List filter) throws Exception {
if(srcFile.exists()==false) {
throw new Exception("压缩目录不存在!");
}
if(srcFile.isDirectory()) {
File[] files = srcFile.listFiles();
base = base.length() == 0 ? "" : base + "/";
if (isExist(base, filter)) {
out.putNextEntry(new ZipEntry(base));
}
for(int i=0; i
zip(out,files[i],base + files[i].getName(),filter);
}
} else {
if (isExist(base, filter)) {
base = base.length() == 0 ? srcFile.getName() : base ;
ZipEntry zipEntry = new ZipEntry(base);
zipEntry.setComment(comment);
out.putNextEntry(zipEntry);
FileInputStream in = new FileInputStream(srcFile);
int length = 0;
byte[] b = new byte[1024];
while((length=in.read(b,0,1024))!=-1) {
out.write(b,0,length);
}
in.close();
}
}
}
/**
* 过滤出要压缩的文件夹
* @param base
* @param list
* @return
*/
public boolean isExist(String base, List list) {
if (list != null && !list.isEmpty()) {
for (int i = 0; i < list.size(); i++) {
if (base.indexOf((String) list.get(i)) >= 0) {
return true;
}
}
}
return false;
}
/**
* @param srcFile:压缩文件路径
* @param dest:解压到的目录
* @param deleteFile:解压完成后是否删除文件
* @throws Exception
*/
public void unZip(String srcFile,String dest,boolean deleteFile) throws Exception {
File file = new File(srcFile);
if(!file.exists()) {
}
ZipFile zipFile ;
zipFile = new ZipFile(srcFile, "GB18030"); //这一句是解决中文乱码的关键
Enumeration e = zipFile.getEntries();
while(e.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry)e.nextElement();
if(zipEntry.isDirectory()) {
String name = zipEntry.getName();
name = name.substring(0,name.length()-1);
File f = new File(dest + name);
//System.out.println("+++"+f.getName());
f.mkdirs();
} else {
File f = new File(dest + zipEntry.getName());
//System.out.println("_______"+f.getName());
f.getParentFile().mkdirs();
f.createNewFile();
InputStream is = zipFile.getInputStream(zipEntry);
FileOutputStream fos = new FileOutputStream(f);
int length = 0;
byte[] b = new byte[1024];
while((length=is.read(b, 0, 1024))!=-1) {
fos.write(b, 0, length);
}
is.close();
fos.close();
}
}
if (zipFile != null) {
zipFile.close();
}
if(deleteFile) {
file.deleteOnExit();
}
}
/**
* 获取Zip文件的注释信息
* @param srcFile
* @return
*/
public static String getZipComment(String srcFile) {
String comment = "";
try {
ZipFile zipFile = new ZipFile(srcFile);
Enumeration e = zipFile.getEntries();
while (e.hasMoreElements()) {
ZipEntry ze = (ZipEntry) e.nextElement();
comment = ze.getComment();
if (comment != null && !comment.equals("")
&& !comment.equals("null")) {
break;
}
}
zipFile.close();
} catch (Exception e) {
System.out.println("获取zip文件注释信息失败:" + e.getMessage());
}
return comment;
}
/**
* desPath保存路劲
* srcPath原路径
* 将图片文件重一个文件夹 移动到一个文件夹
*/
public static void moveImageFile(String srcPath , String desPath)
{
File f = new File(srcPath);
if (!f.exists()) {
System.out.println(srcPath + " not exists");
return;
}
File fa[] = f.listFiles();
for (int i = 0; i < fa.length; i++) {
File fs = fa[i];
if (fs.isDirectory()) {
//目录
System.out.println(fs.getName() + " [目录]");
} else {
//文件(图片,音频,视频)
System.out.println(fs.getName());
String suffix = fs.getName().substring(
fs.getName().lastIndexOf("."));// 文件后缀名
//图片
if(".jpg".equalsIgnoreCase(suffix))
{
String tempurl = msgManager1
.getMessage("pic.path.temp");
//tempurl :E:\\neweclipsewprkspace\\toponymcensus\\app\\temp\\image\\
System.out.println("tempurl :" +tempurl);
String srcImgePath = srcPath+"\\"+fs.getName();
String desImagePath=srcPath+"\\"+"11"+"\\"+fs.getName();
File file = new File(srcImgePath);
try {
if(file.exists())
FileUtil.copyFile(srcImgePath, desImagePath);
if (file.exists()) {
//得到文件保存路劲
// 生成缩略图
String thumbnailsuffix = msgManager1
.getMessage("preview_pic_suffix");
String thumImagePath = desImagePath.replace(".", thumbnailsuffix+".");
PicUtil.createPreviewImage(srcImgePath,thumImagePath);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//视频
if(".mp4".equalsIgnoreCase(suffix))
{
String srcVideoPath = desPath+""+fs.getName();
String desVideopath = "";
File file = new File(srcVideoPath);
try {
String tempurl = msgManager1
.getMessage("video.path.temp");
//videourl :E:\\neweclipsewprkspace\\toponymcensus\\app\\temp\\audio\\
if(file.exists())
FileUtil.copyFile(srcVideoPath, desVideopath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//音频
if(".3gp".equalsIgnoreCase(suffix))
{
String srcAudioPath = srcPath+""+fs.getName();
String desAudioPath = desPath+""+fs.getName();
File file = new File(srcAudioPath);
try {
if(file.exists())
FileUtil.copyFile(srcAudioPath, desAudioPath);
String tempurl = msgManager1
.getMessage("audio.path.temp");//文件的保存路径
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
/**
* 删除文件夹下面所有文件及目录
* @param delpath
*/
public static void deleteFiles(String delpath)
{
try {
File file = new File(delpath);
// 当且仅当此抽象路径名表示的文件存在且 是一个目录时,返回 true
if (!file.isDirectory())
{
file.delete();
}
else if (file.isDirectory())
{
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++)
{
File delfile = new File(delpath + "\\" + filelist[i]);
if (!delfile.isDirectory())
{
delfile.delete();
System.out.println(delfile.getAbsolutePath() + "删除文件成功");
}
else if (delfile.isDirectory())
{
deleteFiles(delpath + "\\" + filelist[i]);
}
}
System.out.println(file.getAbsolutePath()+"删除成功");
file.delete();
}
} catch (Exception e) {
System.out.println("deletefile() Exception:" + e.getMessage());
}
}
public static void main(String[] args) throws Exception {
//文件解压
//ZipUtil tz = new ZipUtil();
//tz.unZip("e://fuck.zip", "e://temp//", false);
//文件复制
String path = "C:\\Users\\Qt\\Desktop\\测试文件";
ZipUtil.moveImageFile(path, "");
//String path = "C:\\Users\\Qt\\Desktop\\测试文件\\11";
//ZipUtil.deleteFiles(path);
}
}
java zip文件操作,java 关于 zip 文件 的 基本操作相关推荐
- 使用文件操作函数实现:文件的复制功能。
2019独角兽企业重金招聘Python工程师标准>>> //使用文件操作函数实现:文件的复制功能. #include <stdio.h> #include <std ...
- matlab读int16读文件_Matlab文件操作及读txt文件(fopen,fseek,fread,fclose)
Matlab 文件操作及读 txt 文件 (fopen,fseek,fread,fclose) matlab 文件操作 文件操作是一种重要的输入输出方式,即从数据文件读取数据或将 结果写入数据文件. ...
- C# 对Ini文件操作(C# ini文件操作类)
*************************************************** 更多精彩,欢迎进入:http://shop115376623.taobao.com ****** ...
- 简述python文件操作的流程_Python文件操作详解
这篇文章主要介绍了Python 文件操作的详解及实例的相关资料,希望通过本文大家能够理解掌握Python 文件操作的知识,需要的朋友可以参考下 Python 文件操作的详解及实例 一.文件操作 1.对 ...
- fclose用法matlab,matlab文件操作及读txt文件(fopen,fseek,fread,fclose
matlab文件操作及读txt文件(fopen,fseek,fread,fclose) matlab文件操作 文件操作是一种重要的输入输出方式,即从数据文件读取数据或将结果写入数据文件.MATLAB提 ...
- linux c文件操作,C语言文件操作(FILE)与常用文件操作函数
文件 1.文件基本概念 C程序把文件分为ASCII文件和二进制文件,ASCII文件又称文本文件,二进制文件和文本文件(也称ASCII码文件)二进制文件中,数值型数据是以二进制形式存储的, 而在文本文件 ...
- 【Android 安全】DEX 加密 ( 代理 Application 开发 | 解压 apk 文件 | 判定是否是第一次启动 | 递归删除文件操作 | 解压 Zip 文件操作 )
文章目录 一.判定是否是第一次启动 二.递归删除文件操作 三.解压 Zip 文件操作 四.解压操作相关代码 参考博客 : [Android 安全]DEX 加密 ( 常用 Android 反编译工具 | ...
- 【Java文件操作】一个将文件转化为十六进制字节数组的工具(可以以8086汇编查看内存的格式输出,很优雅)
将文件转化为字节数组 写了一个方法,直接用即可 /*** 将文件转化成字节数组** @param 要转换的文件路径* @return 转换成的字节数组* @throws IOException*/pu ...
- python压缩文件操作_python处理zip压缩文件 | 学步园
从简单的角度来看的话,zip格式会是个不错的选择,而且python对zip格式的支持够简单,够好用. 1)简单应用 如果你仅仅是希望用python来做压缩和解压缩,那么就不用去翻文档了,这里提供一个简 ...
最新文章
- 【经典书】随机矩阵理论与无线网络
- 自考之——我看《软件开发工具》
- OSPF 多区域配置
- JavaScript---设计模式之观察者模式
- 使用mp4v2将aac音频h264视频数据封装成mp4开发心得
- 经纬度坐标系转东北天_大地坐标系(WGS-84)、地心地固坐标系(ECEF)与东北天坐标系(ENU)的相互转换C语言代码分享...
- web程序前后台功能实现_好程序员web前端教程之JS继承实现方式解析
- 《GPUPro》笔记
- AFNnetworking详解
- 未来茅台酒会怎样跌下神坛?
- 微信小程序开发的坑---路由参数
- 设计模式必须遵守的六大原则
- 空气质量模型:操作指南与案例研究(模型概述)
- SAP基本计量单位更改
- Java征兵系统(可拓展)
- 手机上如何使用Termux当终端,以及开启SSH服务的步骤
- 感谢有你!Apache DolphinScheduler 项目 GitHub star 突破 8k
- 中国互联网金融:浪潮还是浪花?
- Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
- 阿菜的Vue学习之旅(一)
热门文章
- 单片机串口发送数据很慢?这种方法帮助你提高!
- oracle audit文件,[20191128]oracle Audit文件管理2.txt
- java队列等待唤醒_Java深入学习29:线程等待和唤醒的两个方案
- linux prc 时区,授时时区问题解决
- 20220207-CTF-MISC-第11题--- base64隐写--附带脚本
- python数据分析基础教程考试试卷_Python数据分析-自测试卷5
- CentOS6.x 下 /etc/security/limits.conf 被改错的故障经历
- SQL优化:从设计表结构开始(层次型表结构设计方法)
- C# WINFORM的自动更新程序
- UNIX网络编程——解决TCP网络传输“粘包”问题