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("\\*", "/");
;
//判斷路徑是否存在,不存在則創(chuàng)建文件路徑
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if (!file.exists()) {
file.mkdirs();
}
//判斷文件全路徑是否為文件夾,如果是上面已經(jīng)上傳,不需要解壓
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("復(fù)制文件:" + "\n" + "源路徑:" + url1 + "\n" + "目標(biāo)路徑:"
+ url2);
while ((n = in.read(buff)) != -1) {
out.write(buff, 0, n);
}
out.flush();
in.close();
out.close();
System.out.println("復(fù)制完成");
}
}