package com.jesse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;
public class Jieya {
public static int zipNum = 0;
static String path = "F:\\英语"; // 路径
public static void main(String args[]) throws IOException{
getFile(path,false);
}
public static void getFile(String initPath,boolean isDecompressionDirctory) {
File allFile = new File(initPath);
if (!allFile.exists()) {
System.out.println(initPath + " not exists");
return;
}
//列出总文件夹下所有文件及文件夹
File allFileArr[] = allFile.listFiles();
for (int i = 0; i < allFileArr.length; i++) {
File fs = allFileArr[i];
//处理当前文件为文件夹
if (fs.isDirectory()) {
File nextFa[] = fs.listFiles();
String currentPath = initPath + "\\" + fs.getName();
if(nextFa.length > 0){
getFile(currentPath,false);
}
}
//文件名称包含 动漫测试时不处理
if(fs.getName().contains("动漫测试")){
}
//处理当前文件为zip
else if(fs.getName().contains(".zip")){
//正常解压
File zipFile = new File(initPath + "\\" + fs.getName());
try {
//处理多层解压后文件夹下面有文件夹
String fileName = fs.getName();
//获取解压后压缩包路径
String unZipPath = initPath + "\\" +fileName;
int index = unZipPath.indexOf(".");
unZipPath = unZipPath.substring(0, index).replaceAll("\\*", "/");// 1
//解压文件
upzipFile(zipFile,unZipPath);
//读取解压后压缩包的路径
DecompressionDirctory(unZipPath,false);
} catch (IOException e) {
e.printStackTrace();
}
}
//处理当前文件为rar
else if(fs.getName().contains(".rar")){
//正常解压
File rarFile = new File(initPath + "\\" + fs.getName());
try {
//处理多层解压后文件夹下面有文件夹
String fileName = fs.getName();
//获取解压后压缩包路径
String unRarPath = initPath + "\\" +fileName;
int index = unRarPath.indexOf(".");
unRarPath = unRarPath.substring(0, index).replaceAll("\\*", "/");// 1
//解压
unRarFile(rarFile.toString(),unRarPath);
//读取解压后压缩包的路径
DecompressionDirctory(unRarPath,false);
} catch (IOException e) {
e.printStackTrace();
}
}
else{
}
}
}
public static void DecompressionDirctory(String unZipPath,boolean isCopy) throws IOException{
File allFile = new File(unZipPath);
if (!allFile.exists()) {
System.out.println(unZipPath + " not exists----DecompressionDirctory");
return;
}
//列出总文件夹下所有文件及文件夹
File allFileArr[] = allFile.listFiles();
if(allFileArr != null && allFileArr.length > 0){
for (int i = 0; i < allFileArr.length; i++) {
File fs = allFileArr[i];
//处理当前文件为文件夹
if (fs.isDirectory()) {
System.out.println(fs.getName() + " [目录]");
File nextFa[] = fs.listFiles();
String currentPath = unZipPath + "\\" + fs.getName();
if(nextFa.length > 0){
DecompressionDirctory(currentPath,true);
}
}
//文件名称包含 动漫测试时不处理
if(fs.getName().contains("动漫测试")){
}
//处理当前文件为zip
else if(fs.getName().contains("zip")){
//正常解压
File zipFile = new File(unZipPath + "\\" + fs.getName());
try {
//处理多层解压后文件夹下面有文件夹
String fileName = fs.getName();
//获取解压后压缩包路径
String newunZipPath = unZipPath + "\\" +fileName;
int index = newunZipPath.indexOf(".");
newunZipPath = newunZipPath.substring(0, index).replaceAll("\\*", "/");// 1
//解压文件
upzipFile(zipFile,unZipPath);
//读取解压后压缩包的路径
DecompressionDirctory(newunZipPath,true);
} catch (IOException e) {
e.printStackTrace();
}
}
//处理当前文件为rar
else if(fs.getName().contains("rar")){
}
//
else{
if(isCopy){
int index = fs.toString().lastIndexOf("\\");
String oldPath = fs.toString().substring(0, index);
String newPath = oldPath.substring(0, oldPath.lastIndexOf("\\"));
copy(oldPath,newPath);
}
}
}
}
}
/* 对.zip文件进行解压缩
* @param zipFile 解压缩文件
* @param descDir 压缩的目标地址,如:D:\\测试 或 /mnt/d/测试
* @return
*/
@SuppressWarnings("rawtypes")
public static List<File> upzipFile(File zipFile, String descDir) {
List<File> _list = new ArrayList<File>() ;
byte[] _byte = new byte[1024] ;
try {
ZipFile _zipFile = new ZipFile(zipFile , "GBK") ;
for( Enumeration entries = _zipFile.getEntries() ; entries.hasMoreElements() ; ){
ZipEntry entry = (ZipEntry)entries.nextElement() ;
File _file = new File(descDir + File.separator + entry.getName()) ;
if( entry.isDirectory() ){
_file.mkdirs() ;
}else{
File _parent = _file.getParentFile() ;
if( !_parent.exists() ){
_parent.mkdirs() ;
}
InputStream _in = _zipFile.getInputStream(entry);
OutputStream _out = new FileOutputStream(_file) ;
int len = 0 ;
while( (len = _in.read(_byte)) > 0){
_out.write(_byte, 0, len);
}
_in.close();
_out.flush();
_out.close();
_list.add(_file) ;
}
}
} catch (IOException e) {
}
return _list ;
}
/**
* 根据原始rar路径,解压到指定文件夹下.
* @param srcRarPath 原始rar路径
* @param dstDirectoryPath 解压到的文件夹
*/
public static void unRarFile(String srcRarPath, String dstDirectoryPath) {
if (!srcRarPath.toLowerCase().endsWith(".rar")) {
System.out.println("非rar文件!");
return;
}
File dstDiretory = new File(dstDirectoryPath);
if (!dstDiretory.exists()) {// 目标目录不存在时,创建该文件夹
dstDiretory.mkdirs();
}
Archive a = null;
try {
a = new Archive(new File(srcRarPath));
if (a != null) {
a.getMainHeader().print(); // 打印文件信息.
FileHeader fh = a.nextFileHeader();
while (fh != null) {
if (fh.isDirectory()) { // 文件夹
String fileName= fh.getFileNameW().trim();
if(fh.isUnicode()){
fileName = fh.getFileNameW();
}else{
fileName = fh.getFileNameString().trim();
}
File fol = new File(dstDirectoryPath + File.separator + fileName);
fol.mkdirs();
} else { // 文件
String fileName= fh.getFileNameW().trim();
if(fh.isUnicode()){
fileName = fh.getFileNameW();
}else{
fileName = fh.getFileNameString().trim();
}
File out = new File(dstDirectoryPath + File.separator
+ fileName);
//System.out.println(out.getAbsolutePath());
try {// 之所以这么写try,是因为万一这里面有了异常,不影响继续解压.
if (!out.exists()) {
if (!out.getParentFile().exists()) {// 相对路径可能多级,可能需要创建父目录.
out.getParentFile().mkdirs();
}
out.createNewFile();
}
FileOutputStream os = new FileOutputStream(out);
a.extractFile(fh, os);
os.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
fh = a.nextFileHeader();
}
a.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 复制文件
* @param oldPath
* @param newPath
* @throws IOException
*/
public static void copy(String oldPath,String newPath) throws IOException{
File oldDir = new File(oldPath);
File newDir = new File(newPath);
FileInputStream fis = null;
FileOutputStream fos = null;
//用字节输入输出流
File[] file = oldDir.listFiles();
for(int i=0;i<file.length;i++){
if(file[i].isDirectory()){
}else{
String path = file[i].getAbsolutePath();
String filename = path.substring(path.lastIndexOf("\\")+1, path.length());
File AfterFile = new File(newDir+ "\\"+filename+"");
if(!AfterFile.exists()){
AfterFile.createNewFile();
}//如果文件不存在,则进行创建
fis = new FileInputStream(file[i]);
fos = new FileOutputStream(AfterFile);
byte[] buffer = new byte[1024];
int length;
while((length=fis.read(buffer))!=-1){
fos.write(buffer,0,length);
}
}
}
fis.close();
fos.close();
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;
public class Jieya {
public static int zipNum = 0;
static String path = "F:\\英语"; // 路径
public static void main(String args[]) throws IOException{
getFile(path,false);
}
public static void getFile(String initPath,boolean isDecompressionDirctory) {
File allFile = new File(initPath);
if (!allFile.exists()) {
System.out.println(initPath + " not exists");
return;
}
//列出总文件夹下所有文件及文件夹
File allFileArr[] = allFile.listFiles();
for (int i = 0; i < allFileArr.length; i++) {
File fs = allFileArr[i];
//处理当前文件为文件夹
if (fs.isDirectory()) {
File nextFa[] = fs.listFiles();
String currentPath = initPath + "\\" + fs.getName();
if(nextFa.length > 0){
getFile(currentPath,false);
}
}
//文件名称包含 动漫测试时不处理
if(fs.getName().contains("动漫测试")){
}
//处理当前文件为zip
else if(fs.getName().contains(".zip")){
//正常解压
File zipFile = new File(initPath + "\\" + fs.getName());
try {
//处理多层解压后文件夹下面有文件夹
String fileName = fs.getName();
//获取解压后压缩包路径
String unZipPath = initPath + "\\" +fileName;
int index = unZipPath.indexOf(".");
unZipPath = unZipPath.substring(0, index).replaceAll("\\*", "/");// 1
//解压文件
upzipFile(zipFile,unZipPath);
//读取解压后压缩包的路径
DecompressionDirctory(unZipPath,false);
} catch (IOException e) {
e.printStackTrace();
}
}
//处理当前文件为rar
else if(fs.getName().contains(".rar")){
//正常解压
File rarFile = new File(initPath + "\\" + fs.getName());
try {
//处理多层解压后文件夹下面有文件夹
String fileName = fs.getName();
//获取解压后压缩包路径
String unRarPath = initPath + "\\" +fileName;
int index = unRarPath.indexOf(".");
unRarPath = unRarPath.substring(0, index).replaceAll("\\*", "/");// 1
//解压
unRarFile(rarFile.toString(),unRarPath);
//读取解压后压缩包的路径
DecompressionDirctory(unRarPath,false);
} catch (IOException e) {
e.printStackTrace();
}
}
else{
}
}
}
public static void DecompressionDirctory(String unZipPath,boolean isCopy) throws IOException{
File allFile = new File(unZipPath);
if (!allFile.exists()) {
System.out.println(unZipPath + " not exists----DecompressionDirctory");
return;
}
//列出总文件夹下所有文件及文件夹
File allFileArr[] = allFile.listFiles();
if(allFileArr != null && allFileArr.length > 0){
for (int i = 0; i < allFileArr.length; i++) {
File fs = allFileArr[i];
//处理当前文件为文件夹
if (fs.isDirectory()) {
System.out.println(fs.getName() + " [目录]");
File nextFa[] = fs.listFiles();
String currentPath = unZipPath + "\\" + fs.getName();
if(nextFa.length > 0){
DecompressionDirctory(currentPath,true);
}
}
//文件名称包含 动漫测试时不处理
if(fs.getName().contains("动漫测试")){
}
//处理当前文件为zip
else if(fs.getName().contains("zip")){
//正常解压
File zipFile = new File(unZipPath + "\\" + fs.getName());
try {
//处理多层解压后文件夹下面有文件夹
String fileName = fs.getName();
//获取解压后压缩包路径
String newunZipPath = unZipPath + "\\" +fileName;
int index = newunZipPath.indexOf(".");
newunZipPath = newunZipPath.substring(0, index).replaceAll("\\*", "/");// 1
//解压文件
upzipFile(zipFile,unZipPath);
//读取解压后压缩包的路径
DecompressionDirctory(newunZipPath,true);
} catch (IOException e) {
e.printStackTrace();
}
}
//处理当前文件为rar
else if(fs.getName().contains("rar")){
}
//
else{
if(isCopy){
int index = fs.toString().lastIndexOf("\\");
String oldPath = fs.toString().substring(0, index);
String newPath = oldPath.substring(0, oldPath.lastIndexOf("\\"));
copy(oldPath,newPath);
}
}
}
}
}
/* 对.zip文件进行解压缩
* @param zipFile 解压缩文件
* @param descDir 压缩的目标地址,如:D:\\测试 或 /mnt/d/测试
* @return
*/
@SuppressWarnings("rawtypes")
public static List<File> upzipFile(File zipFile, String descDir) {
List<File> _list = new ArrayList<File>() ;
byte[] _byte = new byte[1024] ;
try {
ZipFile _zipFile = new ZipFile(zipFile , "GBK") ;
for( Enumeration entries = _zipFile.getEntries() ; entries.hasMoreElements() ; ){
ZipEntry entry = (ZipEntry)entries.nextElement() ;
File _file = new File(descDir + File.separator + entry.getName()) ;
if( entry.isDirectory() ){
_file.mkdirs() ;
}else{
File _parent = _file.getParentFile() ;
if( !_parent.exists() ){
_parent.mkdirs() ;
}
InputStream _in = _zipFile.getInputStream(entry);
OutputStream _out = new FileOutputStream(_file) ;
int len = 0 ;
while( (len = _in.read(_byte)) > 0){
_out.write(_byte, 0, len);
}
_in.close();
_out.flush();
_out.close();
_list.add(_file) ;
}
}
} catch (IOException e) {
}
return _list ;
}
/**
* 根据原始rar路径,解压到指定文件夹下.
* @param srcRarPath 原始rar路径
* @param dstDirectoryPath 解压到的文件夹
*/
public static void unRarFile(String srcRarPath, String dstDirectoryPath) {
if (!srcRarPath.toLowerCase().endsWith(".rar")) {
System.out.println("非rar文件!");
return;
}
File dstDiretory = new File(dstDirectoryPath);
if (!dstDiretory.exists()) {// 目标目录不存在时,创建该文件夹
dstDiretory.mkdirs();
}
Archive a = null;
try {
a = new Archive(new File(srcRarPath));
if (a != null) {
a.getMainHeader().print(); // 打印文件信息.
FileHeader fh = a.nextFileHeader();
while (fh != null) {
if (fh.isDirectory()) { // 文件夹
String fileName= fh.getFileNameW().trim();
if(fh.isUnicode()){
fileName = fh.getFileNameW();
}else{
fileName = fh.getFileNameString().trim();
}
File fol = new File(dstDirectoryPath + File.separator + fileName);
fol.mkdirs();
} else { // 文件
String fileName= fh.getFileNameW().trim();
if(fh.isUnicode()){
fileName = fh.getFileNameW();
}else{
fileName = fh.getFileNameString().trim();
}
File out = new File(dstDirectoryPath + File.separator
+ fileName);
//System.out.println(out.getAbsolutePath());
try {// 之所以这么写try,是因为万一这里面有了异常,不影响继续解压.
if (!out.exists()) {
if (!out.getParentFile().exists()) {// 相对路径可能多级,可能需要创建父目录.
out.getParentFile().mkdirs();
}
out.createNewFile();
}
FileOutputStream os = new FileOutputStream(out);
a.extractFile(fh, os);
os.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
fh = a.nextFileHeader();
}
a.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 复制文件
* @param oldPath
* @param newPath
* @throws IOException
*/
public static void copy(String oldPath,String newPath) throws IOException{
File oldDir = new File(oldPath);
File newDir = new File(newPath);
FileInputStream fis = null;
FileOutputStream fos = null;
//用字节输入输出流
File[] file = oldDir.listFiles();
for(int i=0;i<file.length;i++){
if(file[i].isDirectory()){
}else{
String path = file[i].getAbsolutePath();
String filename = path.substring(path.lastIndexOf("\\")+1, path.length());
File AfterFile = new File(newDir+ "\\"+filename+"");
if(!AfterFile.exists()){
AfterFile.createNewFile();
}//如果文件不存在,则进行创建
fis = new FileInputStream(file[i]);
fos = new FileOutputStream(AfterFile);
byte[] buffer = new byte[1024];
int length;
while((length=fis.read(buffer))!=-1){
fos.write(buffer,0,length);
}
}
}
fis.close();
fos.close();
}
}