日韩性视频-久久久蜜桃-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)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 西西大胆午夜视频 | 狠狠爱天天干 | 久久女女| 国产91高清 | 爱情岛亚洲首页论坛 | a天堂资源在线观看 | 自拍偷拍亚洲精品 | www.国产麻豆| 免费人成视频在线播放 | 亚洲国产欧美在线人成 | 快色网站| v天堂在线观看 | 成人一级片视频 | 国产性自拍 | 国产毛片久久久 | 亚洲美女福利 | 国产一区二区三区免费播放 | 可以免费观看的毛片 | 黄色一级国产 | 91免费视频网 | 国产亚洲高清视频 | 涩涩国产 | 不卡一区二区在线 | 国产精品久久久久一区二区三区 | 欧美日韩中文字幕一区 | 色秀视频在线观看 | 欧美最顶级a∨艳星 | 精品一区二区三区蜜桃 | 色久月 | 欧美在线一级片 | 亚洲午夜无码av毛片久久 | 色婷婷在线播放 | 大又大粗又爽又黄少妇毛片 | 四虎黄色片 | 亚洲欧美日本一区二区三区 | 美日韩av| 国产二区一区 | 日韩二三区| 久草福利资源站 | 亚洲婷婷一区 | 鲁大师私人影院在线观看 | 一区二区三区亚洲 | 中文字幕日韩在线播放 | 色盈盈影院 | 长篇h版少妇沉沦交换 | 成年女人毛片 | 日本精品中文字幕 | 日本一二三区在线 | 国产免费一区二区三区三州老师 | 美女福利网站 | 日韩毛片视频 | 亚洲精品国产精品乱码 | 麻豆成人入口 | 国产精品三级在线观看无码 | 天天躁日日躁狠狠躁伊人 | 国产成人免费观看 | 久久久久久爱 | 久草成人在线视频 | 男人爱看的网站 | 激情成人av| 欲色影音 | 黑帮大佬和我的三百六十五天 | 国产精品国产精品国产专区 | 男同互操gay射视频在线看 | 久久久久久蜜桃 | 男女做激情爱呻吟口述全过程 | 亚洲涩综合 | 国产欧美123 | 黄色日韩在线 | 在线免费三级 | 在线看毛片网站 | 麻豆免费在线视频 | 亚洲精品视频网 | 日本做受| 秋葵视频在线 | 婷婷影院在线观看 | 婷婷综合另类小说色区 | 国产卡一卡二卡三无线乱码新区 | 精品乱子伦一区二区三区 | 成人区人妻精品一区二区网站 | 黄色a级片网站 | 中文字幕在线播放 | 嫩草影院菊竹影院 | 日本高清视频在线观看 | 精品久久久久久一区二区里番 | 人人干干| 欧美精品在线免费 | 爽爽爽av | 成 人 黄 色 片 在线播放 | 成人久久影院 | 人人干免费 | 9i在线看片成人免费 | 久久人体 | 综合成人 | 在线观看免费中文字幕 | 日韩av免费在线 | 激情小说图片视频 | 亚洲一区精品视频在线观看 | 国产日韩一区二区在线观看 |