日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java文件解压文件_java 文件解压缩

發(fā)布時間:2023/12/1 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java文件解压文件_java 文件解压缩 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

直接上代碼:

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;

/*

* 對特定文件進(jìn)行解壓縮

*/

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);

//創(chuàng)建指定目錄,如果其不存在的情況下

if (!desFile.exists()) {

if (!desFile.mkdirs()) {

System.out.println("指定目錄創(chuàng)建失敗");

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()) {

//創(chuàng)建目錄

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 壓縮文件結(jié)果的存儲路徑,如果輸入路徑為空,或者存儲路徑已經(jīng)存在,則返回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);

// 判斷目標(biāo)目錄是否存在,如果不存在就創(chuàng)建

if (!desFile.exists()) {

if (!desFile.mkdirs()) {

System.out.println("存儲目錄創(chuàng)建失敗");

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();

}

}

}

/*

* 路徑轉(zhuǎn)換成相對路徑 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) + "/";

}

}

總結(jié)

以上是生活随笔為你收集整理的java文件解压文件_java 文件解压缩的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。