java文件解压文件_java 文件解压缩
直接上代碼:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
/*
* 對特定文件進行解壓縮
*/
public class ZipAndUnZipUtil {
private ZipAndUnZipUtil() {
}
private static ZipAndUnZipUtil zipSingleInstance;
/*
* 生成實例
*/
public static ZipAndUnZipUtil getInstance() {
if (null == zipSingleInstance) {
zipSingleInstance= new ZipAndUnZipUtil();
}
return zipSingleInstance;
}
/*
* 解壓文件
* @param srcPath 源壓縮包路徑
* @param despath 解壓后存儲的目錄
* @return 解壓后存儲的路徑目錄
*/
@SuppressWarnings("unchecked")
public String unZip(String srcPath, String desPath) {
if ("".equals(srcPath) || null == srcPath) {
System.out.println("解壓的文件的路徑為空");
return null;
}
if ("".equals(srcPath) || null == desPath) {
System.out.println("文件解壓后存儲路徑為");
return null;
}
File srcFile = new File(srcPath);
File desFile = new File(desPath);
//創建指定目錄,如果其不存在的情況下
if (!desFile.exists()) {
if (!desFile.mkdirs()) {
System.out.println("指定目錄創建失敗");
return null;
}
}
try {
//讀取zip文件
ZipFile zipTest = new ZipFile(srcFile);
Enumeration entries = (Enumeration) zipTest
.entries();
ZipEntry entry = null;
while (entries.hasMoreElements()) {
entry = entries.nextElement();
if (entry.isDirectory()) {
//創建目錄
File dirFile=new File(desPath+File.separator+entry.getName());
dirFile.mkdirs();
} else {
//解壓文件
BufferedInputStream inputStream = new BufferedInputStream(zipTest.getInputStream(entry));
BufferedOutputStream out =new BufferedOutputStream( new FileOutputStream(desPath+File.separator+entry.getName()));
byte[] bytes=new byte[2*1024];
int count;
while((count=inputStream.read(bytes))!=-1){
out.write(bytes, 0, count);
}
inputStream.close();
out.close();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return desPath;
}
/*
* 壓縮文件
*
* @param srcPath 需要壓縮的文件路徑
* @param despath 壓縮文件的存儲目錄
* @param name 文件名字
* @return desPath 壓縮文件結果的存儲路徑,如果輸入路徑為空,或者存儲路徑已經存在,則返回null,
*/
public String zipDocuments(String srcPath, String desPath,String name) {
if ("".equals(srcPath) || null == srcPath) {
System.out.println("壓縮的文件的路徑為空");
return null;
}
if ("".equals(srcPath) || null == desPath) {
System.out.println("壓縮文件的存儲路徑為");
return null;
}
File srcFile = new File(srcPath);
File desFile = new File(desPath);
// 判斷目標目錄是否存在,如果不存在就創建
if (!desFile.exists()) {
if (!desFile.mkdirs()) {
System.out.println("存儲目錄創建失敗");
return null;
}
}
try {
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(
desPath + File.separator + name));
int start = srcPath.lastIndexOf(srcFile.getName());
// 壓縮文件
zipDirAndFile(srcFile, outZip, start);
outZip.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return desPath + File.separator + name;
}
/*
* 壓縮文件及目錄
*/
private void zipDirAndFile(File file, ZipOutputStream out, int start) {
if (null == file | null == out) {
throw new NullPointerException("文件或ZipOutputStream引用為空");
}
File[] files;
// 如果file對象是目錄
if (file.isDirectory()) {
files = file.listFiles();
try {
// 生成壓縮目錄文件
String dirPath = file.getPath().substring(start);
out.putNextEntry(new ZipEntry(absToreDirPath(dirPath)));
out.closeEntry();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (File f : files) {
zipDirAndFile(f, out, start);
}
} else {
// 如果file對象時文件
BufferedInputStream inZip;
byte[] buff;
try {
inZip = new BufferedInputStream(new FileInputStream(file));
buff = new byte[5 * 1024];
ZipEntry zipEntry = new ZipEntry(new String(file.getPath()
.substring(start)));
out.putNextEntry(zipEntry);
int count;
while ((count = inZip.read(buff, 0, buff.length)) != -1) {
out.write(buff, 0, count);
}
inZip.close();
out.closeEntry();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* 路徑轉換成相對路徑 start:相對路徑起始的位置
*/
private String absToreDirPath(String absPath) {
char[] pathChars = absPath.toCharArray();
for (int i = 0; i < pathChars.length; i++) {
if (File.separatorChar == pathChars[i]) {
pathChars[i] = '/';
}
}
return new String(pathChars) + "/";
}
}
總結
以上是生活随笔為你收集整理的java文件解压文件_java 文件解压缩的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做男性不育检需到南昌哪家医院好
- 下一篇: 冻胚多少钱啊?