java实现多个文件压缩成压缩包
作者:NFA_YY 时间:2022-12-06 17:45:54
本文实例为大家分享了java实现多个文件压缩的具体代码,供大家参考,具体内容如下
需要用到的ant.jar
package util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class ZipTools {
public static final String ZIP_FILENAME = ""; //需要解压缩的文件名
public static final String ZIP_DIR = ""; //需要压缩的文件夹
public static final String UN_ZIP_DIR = ""; //要解压的文件目录
public static final int BUFFER = 1024 ; //缓存大小
public static void zipFile(String baseDir,String fileName) throws Exception{
List fileList=getSubFiles(new File(baseDir));
ZipOutputStream zos=new ZipOutputStream(new FileOutputStream(fileName));
ZipEntry ze=null;
byte[] buf=new byte[BUFFER];
int readLen=0;
for(int i = 0; i <fileList.size(); i++) {
File f=(File)fileList.get(i);
ze=new ZipEntry(getAbsFileName(baseDir, f));
ze.setSize(f.length());
ze.setTime(f.lastModified());
zos.putNextEntry(ze);
InputStream is=new BufferedInputStream(new FileInputStream(f));
while ((readLen=is.read(buf, 0, BUFFER))!=-1) {
zos.write(buf, 0, readLen);
}
is.close();
}
zos.close();
}
private static String getAbsFileName(String baseDir, File realFileName){
File real=realFileName;
File base=new File(baseDir);
String ret=real.getName();
while (true) {
real=real.getParentFile();
if(real==null)
break;
if(real.equals(base))
break;
else
ret=real.getName()+"/"+ret;
}
return ret;
}
private static List getSubFiles(File baseDir){
List ret=new ArrayList();
File[] tmp=baseDir.listFiles();
for (int i = 0; i <tmp.length; i++) {
if(tmp[i].isFile())
ret.add(tmp[i]);
if(tmp[i].isDirectory())
ret.addAll(getSubFiles(tmp[i]));
}
return ret;
}
public static void upZipFile() throws Exception{
ZipFile zfile=new ZipFile(ZIP_FILENAME);
Enumeration zList=zfile.entries();
ZipEntry ze=null;
byte[] buf=new byte[1024];
while(zList.hasMoreElements()){
ze=(ZipEntry)zList.nextElement();
if(ze.isDirectory()){
File f=new File(ZIP_DIR+ze.getName());
f.mkdir();
continue;
}
OutputStream os=new BufferedOutputStream(new FileOutputStream(getRealFileName(ZIP_DIR, ze.getName())));
InputStream is=new BufferedInputStream(zfile.getInputStream(ze));
int readLen=0;
while ((readLen=is.read(buf, 0, 1024))!=-1) {
os.write(buf, 0, readLen);
}
is.close();
os.close();
}
zfile.close();
}
public static File getRealFileName(String baseDir, String absFileName){
String[] dirs=absFileName.split("/");
File ret=new File(baseDir);
if(dirs.length>1){
for (int i = 0; i < dirs.length-1;i++) {
ret=new File(ret, dirs[i]);
}
if(!ret.exists())
ret.mkdirs();
ret=new File(ret, dirs[dirs.length-1]);
return ret;
}
return ret;
}
public static void deleteDirFile(String path){
File file=new File(path);
if(file.isDirectory()){ //如果是目录,先递归删除
String[] list=file.list();
for(int i=0;i<list.length;i++){
deleteDirFile(path+"\\"+list[i]); //先删除目录下的文件
}
}
file.delete();
}
public static String newFolder(String dir){
java.io.File myFilePath=new java.io.File(dir);
if(myFilePath.isDirectory()){}else{
myFilePath.mkdirs();
}
return dir;
}
public static String getFileNames(String path){
File file = new File(path); // get file list where the path has
File[] array = file.listFiles(); // 获得文件列表
String pdfNames="";
for(int i=0;i<array.length;i++){
if(array[i].isFile()){
if(array[i].getName().endsWith(".pdf")){ //获得pdf文件名称
pdfNames+=array[i].getName().substring(0,array[i].getName().length()-4)+",";
}
}
}
if(pdfNames.length()>0){
pdfNames.substring(0,pdfNames.length()-1);
}
return pdfNames;
}
public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数文件大小
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("copy file error!");
e.printStackTrace();
}
}
public static boolean fileExist(String fileNames,String pdfName){
boolean flag=false;
if(!"".equals(fileNames)){
String[] nameArr=fileNames.split(",");
for(int i=0;i<nameArr.length;i++){
if(pdfName.equals(nameArr[i])){ //如果文件名相同则执行拷贝操作(拷贝到zip目录准备打包)
flag=true;
break;
}
}
}
return flag;
}
public static void deleteFileAndDir(String path){
File file = new File(path);
File[] array = file.listFiles();
for(int i=0;i<array.length;i++){
if(array[i].isFile()){
array[i].delete();
}else if(array[i].isDirectory()){
deleteDirFile(array[i].getPath());
}
}
}
}
来源:https://blog.csdn.net/NFA_YY/article/details/73613406
标签:java,文件压缩,压缩包
0
投稿
猜你喜欢
SpringBoot整合Jackson超详细用法(附Jackson工具类)
2023-05-31 23:56:06
基于JavaMail实现邮件发送
2022-06-21 02:28:06
java web监听器统计在线用户及人数
2023-12-14 14:42:52
C#中var关键字用法分析
2022-02-11 15:01:43
获取wince mac地址与IP地址解决方案
2022-01-21 02:04:19
Log4Net 日志配置[附带源码下载]
2023-11-10 04:21:22
C#统计字符串的方法
2021-06-12 17:00:12
LINQ基础之From和GroupBy子句
2023-06-09 02:46:29
@Autowired注解注入的xxxMapper报错问题及解决
2022-10-01 10:31:02
Android Flutter实现GIF动画效果的方法详解
2023-02-06 02:46:37
java实现多线程之定时器任务
2021-07-10 08:56:14
C#中String和StringBuilder的简介与区别
2021-10-13 06:40:54
C#同步网络时间的方法实例详解
2021-10-24 15:47:47
Android通过LIstView显示文件列表的两种方法介绍
2022-05-26 00:00:39
C# 设计模式系列教程-策略模式
2022-04-30 16:09:03
java使用influxDB数据库的详细代码
2023-03-04 05:22:27
深入探究Java线程与进程有哪些区别
2023-05-06 13:26:03
微信公众平台开发教程(三) 基础框架搭建
2022-08-12 00:33:50
C#使用Effects给图片增加阴影效果
2023-09-09 14:50:18
Android串口通信apk源码详解(附完整源码)
2021-06-07 04:39:05