未命名表单.png
UZipFile.java
import java.io.*;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* Created by wzj on 2016/9/9.
*/
public class UZipFile {
/**
* 解压到指定目录
*/
public static void unZipFiles(String zipPath, String descDir) throws IOException {
unZipFiles(new File(zipPath), descDir);
}
/**
* 解压文件到指定目录
*/
@SuppressWarnings("rawtypes")
public static void unZipFiles(File zipFile, String descDir) throws IOException {
File pathFile = new File(descDir);
if (!pathFile.exists()) {
pathFile.mkdirs();
}
//解决zip文件中有中文目录或者中文文件
ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));
for (Enumeration entries = zip.entries(); entries.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = zip.getInputStream(entry);
String outPath = (descDir + zipEntryName).replaceAll("\\*", "/");
;
//判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if (!file.exists()) {
file.mkdirs();
}
//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if (new File(outPath).isDirectory()) {
continue;
}
//输出文件路径信息
System.out.println(outPath);
OutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while ((len = in.read(buf1)) > 0) {
out.write(buf1, 0, len);
}
in.close();
out.close();
}
System.out.println("******************解压完毕********************");
}
}
MainProgram.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
public class MainProgram {
public static void main(String[] args) {
String foldNamePath = "./src/lib/";
String[] aarFilePathNameArr = getAllFileName(foldNamePath);
String[] zipFilePathNameArr = getAllFileNewName(foldNamePath);
for (int i = 0; i < aarFilePathNameArr.length; i++) {
try {
copy(aarFilePathNameArr[i],zipFilePathNameArr[i]);
} catch (Exception e) {
e.printStackTrace();
}
}
String[] allUzipFileNewName = getAllUzipFileNewName(zipFilePathNameArr);
for (int i = 0; i < zipFilePathNameArr.length; i++) {
try {
System.out.println("源文件路径: -> " + zipFilePathNameArr[i]);
System.out.println("解压文件路径: -> " + allUzipFileNewName[i]);
System.out.println("============================================= ");
UZipFile.unZipFiles(zipFilePathNameArr[i],allUzipFileNewName[i]);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String[] getAllFileName(String path) {
ArrayList<String> files = new ArrayList<String>();
File file = new File(path);
File[] tempList = file.listFiles();
if (null != tempList && tempList.length > 0){
for (int i = 0; i < tempList.length; i++) {
files.add(tempList[i].getPath());
}
}
String[] aarFileTypeArr = (String[])files.toArray(new String[0]);
return aarFileTypeArr;
}
public static String[] getAllFileNewName(String path) {
ArrayList<String> files = new ArrayList<String>();
File file = new File(path);
File[] tempList = file.listFiles();
if (null != tempList && tempList.length > 0){
for (int i = 0; i < tempList.length; i++) {
String origalPath = tempList[i].getPath();
origalPath = origalPath.substring(0,origalPath.length() - 3) + "zip";
origalPath = origalPath.substring(0,6) + "zip" + origalPath.substring(9);
files.add(origalPath);
}
}
String[] zipFileTypeArr = (String[])files.toArray(new String[0]);
return zipFileTypeArr;
}
private static String[] getAllUzipFileNewName(String[] zipFileArr){
ArrayList<String> files = new ArrayList<String>();
if (null != zipFileArr && zipFileArr.length > 0){
for (int i = 0; i < zipFileArr.length; i++) {
String origalPath = zipFileArr[i];
origalPath = origalPath.substring(0,6) + "uzip" + origalPath.substring(9,origalPath.length() - 4) + "/";
files.add(origalPath);
}
}
String[] uzipFileTypeArr = (String[])files.toArray(new String[0]);
return uzipFileTypeArr;
}
private static void copy(String url1, String url2) throws Exception {
String dirPath = "./src/zip";
File dirFile = new File(dirPath);
if (dirFile.exists()){
dirFile.delete();
}
dirFile.mkdirs();
FileInputStream in = new FileInputStream(new File(url1));
FileOutputStream out = new FileOutputStream(new File(url2));
byte[] buff = new byte[512];
int n = 0;
System.out.println("复制文件:" + "\n" + "源路径:" + url1 + "\n" + "目标路径:"
+ url2);
while ((n = in.read(buff)) != -1) {
out.write(buff, 0, n);
}
out.flush();
in.close();
out.close();
System.out.println("复制完成");
}
}
网友评论