您的位置:首页 > 其它

File 操作

2015-06-19 10:10 381 查看
package net.aorange.comm;

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.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import de.innosystec.unrar.Archive;
import de.innosystec.unrar.rarfile.FileHeader;
/**
* 解压工具类
* @author luo
*
*/
public class ZipUtil {

/**
* 解压文件到当前目录 功能相当于右键 选择解压
* @param zipFile
* @param
* @author gabriel
*/
@SuppressWarnings({ "rawtypes", "resource" })
public static void unZipFiles(File zipFile,String filename)throws IOException{
//得到压缩文件所在目录
String path=zipFile.getAbsolutePath();
path=path.substring(0,path.lastIndexOf("\\"));
// System.out.println("path "+path);
ZipFile zip = new ZipFile(filename);
for(Enumeration entries =zip.entries();
entries.hasMoreElements();){
ZipEntry entry = (ZipEntry)entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = zip.getInputStream(entry);
//outPath输出目录
String outPath = (path+"\\"+zipEntryName).replaceAll("\\*", "/");;
//System.out.println("outPath "+outPath);
//判断路径是否存在,不存在则创建文件路径
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("******************解压完毕********************");
}

/**
* 解压rar格式压缩包。
* 对应的是java-unrar-0.3.jar,但是java-unrar-0.3.jar又会用到commons-logging-1.1.1.jar
*/
/**
*
* @param folder 要解压的文件所在的目录
* 解压后文件所在目录 folder/examId
* @param examId
*/
public static void unrar(String folder,long examId) throws Exception {
Archive a = null;
FileOutputStream fos = null;
try {
a = new Archive(new File(folder+"/"+examId+".rar"));// 要解压的文件
FileHeader fh = a.nextFileHeader();
while (fh != null) {
if (!fh.isDirectory()) {
// 1 根据不同的操作系统拿到相应的 destDirName 和 destFileName
String compressFileName = fh.getFileNameString().trim();
String destFileName = "";// 解压后的文件路径
// String destDirName = "";
// 非windows系统
if (File.separator.equals("/")) {
destFileName = folder+"/"+examId+"\\"+ compressFileName.replaceAll("\\\\", "/");
// destFileName = destFileName +"/"+examId;
// destDirName = destFileName.substring(0,destFileName.lastIndexOf("/"));

} else {// windows系统
destFileName = folder+"/"+examId+"\\"+ compressFileName.replaceAll("/", "\\\\");
// destFileName = destFileName +"/"+examId;
// destDirName = destFileName.substring(0,destFileName.lastIndexOf("\\"));
}
// 2创建文件夹 (解压后文件所在目录)
File dir = new File(folder+"/"+examId);
if (!dir.exists() || !dir.isDirectory()) {
dir.mkdirs();
}
// 3解压缩文件
fos = new FileOutputStream(new File(destFileName));
a.extractFile(fh, fos);
fos.close();
fos = null;
}
fh = a.nextFileHeader();
}
a.close();
a = null;
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (fos != null) {
try {
fos.close();
fos = null;
} catch (Exception e) {
e.printStackTrace();
}
}
if (a != null) {
try {
a.close();
a = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) throws Exception {
/* try {
//unZipFiles(new File("D:\\zip\\8554.rar"),"D:\\zip\\8554.rar");
// \\\\file:119.145.139.104\xyb\test// ////192.168.199.193/考试资料/test/9438/9438.rar
unrar("\\\\192.168.199.203/xyb/vols/1806","\\\\192.168.199.203/xyb/vols/1806/1086");
File src = new File("D:/xyb/vols/8555/45");
File dest = new File("D:/xyb/vols/8555/50");
// copyFolder(src, dest,8555);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} */
}

// public static String[] filterFile = { ".rar", ".xml", ".png",".tp"};
private static long total = 0l;
public static void copyFolder(File srcFolder, File destFolder,long examId)throws Exception {
String[] filterFile = { ".rar", ".xml", ".png",".tp"};
File[] files = srcFolder.listFiles();
for (File file : files) {
if (file.isFile()) {
String pathname;
if (!file.getName().endsWith(".rar")) {
pathname = destFolder + File.separator +"\\"+examId+"\\"+ file.getName();
}else{
pathname = destFolder + File.separator + file.getName();
}
for (String suff : filterFile) {
if (pathname.endsWith(suff)) {
File dest = new File(pathname);
File destPar = dest.getParentFile();
destPar.mkdirs();
if (!dest.exists()) {
dest.createNewFile();
}
copyFile(file, dest);
}
}
} else {
copyFolder(file, destFolder,examId);
}
}
}

/***
* * copy file * * @param src * @param dest * @param status * @throws
* IOException
*/
public static void copyFile(File src, File dest) throws Exception {
FileInputStream input = null;
FileOutputStream outstrem = null;
try {
input = new FileInputStream(src);
String pathname = dest + File.separator + src.getName();
File destFile = new File(pathname);
File destPar = destFile.getParentFile();
destPar.mkdirs();
if (!dest.exists()) {
destFile.createNewFile();
}
outstrem = new FileOutputStream(destFile);
outstrem.getChannel().transferFrom(input.getChannel(), 0,input.available());
total++;
String temp =String.format("\ncopy:%s size:%s to: %s complate: %s", src,src.length(),dest,total);
System.out.print(temp);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
outstrem.flush();
outstrem.close();
input.close();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: