从zip中读取文件 合并到指定的文件中
引入 commons-io 版本可以选择更高的版本
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
具体代码如下:
package work;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
/**
* @author qianxl
*/
public class ZipCopyTools {
public static void batchCopyZipContentToDirectory(String[] source, String des) {
InputStream in = null;
ZipInputStream zin = null;
try {
for (String src : source) {
//读取文件路径
//String path = "C:\\Users\\Administrator\\Desktop\\sql改造\\sql改造补丁\\medext_mm\\temp\\patch_mmmps_medext_1622031243072.zip";
String path = src;
//存储文件目录路径
//String des = "C:/Users/Administrator/Desktop/sql改造/desc/";
ZipFile zf = new ZipFile(path);
//获取zip 文件格式
String zipEncode = getCharsetFromSourceFile(path);
System.out.println(zf.getName() + zipEncode + "fileEncode");
// 文件输入流
in = new BufferedInputStream(new FileInputStream(path));
// zip文件的编码格式
Charset charset = Charset.forName(zipEncode);
// This class implements an input stream filter for reading files in the ZIP file format.
zin = new ZipInputStream(in, Charset.forName(zipEncode));
// 压缩文件实体
ZipEntry ze;
String zipDirectory = path.substring(0, path.lastIndexOf("."));
while ((ze = zin.getNextEntry()) != null) {
// 压缩文件的绝对路径
//String zipFilePath = zipDirectory +"/"+ ze.toString();
// 获取.class .java 文件的压缩格式
// String fileEncode = getCharsetFromSourceFile(zipFilePath);
String fileEncode =zipEncode;
if (ze.toString().endsWith("java") || ze.toString().endsWith("class")) {
BufferedReader br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze), fileEncode));
//
String fieldPath = ze.getName().toString();
//获取zip文件路径
path = fieldPath;
// 获取压缩文件 .java .class 的路径
String directory = fieldPath.substring(0, fieldPath.lastIndexOf("/"));
//创建文件夹
boolean mkdirs = new File(des + directory).mkdirs();
// 目的文件.class .java 文件的路径
path = des + path;
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(path)), fileEncode));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line.toString());
bw.write(line.toString());
bw.write(System.getProperties().getProperty("line.separator"));
}
bw.flush();
bw.close();
br.close();
}
System.out.println();
}
zin.closeEntry();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
close(zin, in);
}
}
public static void main(String[] args) {
String des = "C:/Users/Administrator/Desktop/desc/";
//String source =
String filedesc = "C:\\Users\\Administrator\\Desktop\\descfile\\";
List<File> fileList = new ArrayList<>();
List<File> zips = getSuffixFilePaths(fileList, filedesc, "zip");
// 返回 zip 文件数组值
// System.out.println(zips.get(0).getAbsoluteFile());
String[] src = zips.stream().map(p -> {
return getAvailablePath(p);
}).toArray(String[]::new);
batchCopyZipContentToDirectory(src, des);
}
/**
* getFileDirectory
*
* @param fileList
* @Return getFileDirectory <java.util.List<java.lang.String>>
*/
public static List<String> getFileDirectory(List<File> fileList) {
fileList.stream().forEach(p -> {
p.getPath().substring(0, p.getPath().lastIndexOf("/"));
});
return null;
}
/**
* 获取文件编码
*
* @param fileName
* @return
* @throws IOException https://my.oschina.net/davz/blog/4470673
*/
private static String getCharsetFromSourceFile(String fileName) throws IOException {
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName));
int p = (bin.read() << 8) + bin.read();
String code = null;
switch (p) {
case 0xefbb:
code = "UTF-8";
break;
case 0xfffe:
code = "Unicode";
break;
case 0xfeff:
code = "UTF-16BE";
break;
default:
code = "GBK";
}
return code;
}
/**
* 关闭给定的io流
*
* @url https://blog.csdn.net/u012250875/article/details/78341874
*/
public static void close(Closeable... closes) {
for (Closeable closeable : closes) {
try {
if (closeable != null) {
closeable.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* getAvailablePath
* *@param file
*
* @Return getAvailablePath <java.lang.String>
*/
private static String getAvailablePath(File file) {
return file.getPath().replace('\\', '/');
}
/**
* getSuffixFilePaths
*
* @param fileList
* @param path
* @param fileSuffix
* @Return getSuffixFilePaths <java.util.List<java.io.File>>
*/
public static List<File> getSuffixFilePaths(List<File> fileList, String path, String fileSuffix) {
File file = new File(path);
File[] files = file.listFiles();
if (files == null) {
return null;
} else {
for (int i = 0; i < files.length; i++) {
// 判断是否是文件夹
if (files[i].isDirectory()) {
// 递归调用getFiles方法,得到所有的文件
getSuffixFilePaths(fileList, getAvailablePath(files[i]), fileSuffix);
// 只处理fileSuffix后缀的文档
} else if (files[i].getName().lastIndexOf(fileSuffix) != -1) {
// copyFileAndAddPackageName(files[i]);
// 添加到文件集合中
fileList.add(files[i]);
}
/// System.out.println(files[i].getAbsolutePath().replace('//',
/// '/'));
}
}
return fileList;
}
// 读取xml 文件 并将xml 文件中的文件
}
/**
* 文件的路径
* @param fileName
*/
// 堆栈方式实现
public static void loopDeleteFields(File fileName) {
Stack<File> stack = new Stack<>();
// 存储文件夹
Stack<File> stackDir= new Stack<>();
// stack 初始化
stack.add(fileName);
stackDir.add(fileName);
while (!stack.isEmpty()) {
// 弹出file
File file = stack.pop();
File[] files = file.listFiles();
if (files != null && files.length > 0) {
for (File f : files) {
if (f.isDirectory()) {
stack.add(f);
//存储文件夹 先进后出 FILO
stackDir.add(f);
} else {
f.delete();
}
}
}
}
// 删除文件夹
while (!stackDir.isEmpty()){
File file = stackDir.pop();
file.delete();
}
// System.out.println("删除--");
}
参考的博客 1 .