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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

java zip文件操作,java 关于 zip 文件 的 基本操作

發(fā)布時(shí)間:2023/12/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java zip文件操作,java 关于 zip 文件 的 基本操作 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

這里用的是appache的包? 為了防止文件中文名亂碼問題

java 操作 .zip 文件? 壓縮文件 解壓文件 刪除文件夾等

package com.gaeainfo.toponymbase.util;

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.List;

import org.apache.tools.zip.ZipEntry;

import org.apache.tools.zip.ZipFile;

import org.apache.tools.zip.ZipOutputStream;

public class ZipUtil {

private static MessageManager msgManager1 = new MessageManager(

"bundle.toponymbase");

private String comment = "";

public void setComment(String comment) {

this.comment = comment;

}

/**

* @param src:要壓縮的目錄

* @param dest:壓縮文件存檔

* @throws Exception

*/

public void zip(String src, String dest, List filter) throws Exception {

ZipOutputStream out = new ZipOutputStream(new FileOutputStream(dest));

File srcFile = new File(src);

zip(out,srcFile,"",filter);

out.close();

}

/**

* @param out

* @param srcFile

* @param base:根路徑

* @param filter:過濾

* @throws Exception

*/

public void zip(ZipOutputStream out, File srcFile, String base, List filter) throws Exception {

if(srcFile.exists()==false) {

throw new Exception("壓縮目錄不存在!");

}

if(srcFile.isDirectory()) {

File[] files = srcFile.listFiles();

base = base.length() == 0 ? "" : base + "/";

if (isExist(base, filter)) {

out.putNextEntry(new ZipEntry(base));

}

for(int i=0; i

zip(out,files[i],base + files[i].getName(),filter);

}

} else {

if (isExist(base, filter)) {

base = base.length() == 0 ? srcFile.getName() : base ;

ZipEntry zipEntry = new ZipEntry(base);

zipEntry.setComment(comment);

out.putNextEntry(zipEntry);

FileInputStream in = new FileInputStream(srcFile);

int length = 0;

byte[] b = new byte[1024];

while((length=in.read(b,0,1024))!=-1) {

out.write(b,0,length);

}

in.close();

}

}

}

/**

* 過濾出要壓縮的文件夾

* @param base

* @param list

* @return

*/

public boolean isExist(String base, List list) {

if (list != null && !list.isEmpty()) {

for (int i = 0; i < list.size(); i++) {

if (base.indexOf((String) list.get(i)) >= 0) {

return true;

}

}

}

return false;

}

/**

* @param srcFile:壓縮文件路徑

* @param dest:解壓到的目錄

* @param deleteFile:解壓完成后是否刪除文件

* @throws Exception

*/

public void unZip(String srcFile,String dest,boolean deleteFile) throws Exception {

File file = new File(srcFile);

if(!file.exists()) {

}

ZipFile zipFile ;

zipFile = new ZipFile(srcFile, "GB18030"); //這一句是解決中文亂碼的關(guān)鍵

Enumeration e = zipFile.getEntries();

while(e.hasMoreElements()) {

ZipEntry zipEntry = (ZipEntry)e.nextElement();

if(zipEntry.isDirectory()) {

String name = zipEntry.getName();

name = name.substring(0,name.length()-1);

File f = new File(dest + name);

//System.out.println("+++"+f.getName());

f.mkdirs();

} else {

File f = new File(dest + zipEntry.getName());

//System.out.println("_______"+f.getName());

f.getParentFile().mkdirs();

f.createNewFile();

InputStream is = zipFile.getInputStream(zipEntry);

FileOutputStream fos = new FileOutputStream(f);

int length = 0;

byte[] b = new byte[1024];

while((length=is.read(b, 0, 1024))!=-1) {

fos.write(b, 0, length);

}

is.close();

fos.close();

}

}

if (zipFile != null) {

zipFile.close();

}

if(deleteFile) {

file.deleteOnExit();

}

}

/**

* 獲取Zip文件的注釋信息

* @param srcFile

* @return

*/

public static String getZipComment(String srcFile) {

String comment = "";

try {

ZipFile zipFile = new ZipFile(srcFile);

Enumeration e = zipFile.getEntries();

while (e.hasMoreElements()) {

ZipEntry ze = (ZipEntry) e.nextElement();

comment = ze.getComment();

if (comment != null && !comment.equals("")

&& !comment.equals("null")) {

break;

}

}

zipFile.close();

} catch (Exception e) {

System.out.println("獲取zip文件注釋信息失敗:" + e.getMessage());

}

return comment;

}

/**

* desPath保存路勁

* srcPath原路徑

* 將圖片文件重一個(gè)文件夾 移動到一個(gè)文件夾

*/

public static void moveImageFile(String srcPath , String desPath)

{

File f = new File(srcPath);

if (!f.exists()) {

System.out.println(srcPath + " not exists");

return;

}

File fa[] = f.listFiles();

for (int i = 0; i < fa.length; i++) {

File fs = fa[i];

if (fs.isDirectory()) {

//目錄

System.out.println(fs.getName() + " [目錄]");

} else {

//文件(圖片,音頻,視頻)

System.out.println(fs.getName());

String suffix = fs.getName().substring(

fs.getName().lastIndexOf("."));// 文件后綴名

//圖片

if(".jpg".equalsIgnoreCase(suffix))

{

String tempurl = msgManager1

.getMessage("pic.path.temp");

//tempurl :E:\\neweclipsewprkspace\\toponymcensus\\app\\temp\\image\\

System.out.println("tempurl :" +tempurl);

String srcImgePath = srcPath+"\\"+fs.getName();

String desImagePath=srcPath+"\\"+"11"+"\\"+fs.getName();

File file = new File(srcImgePath);

try {

if(file.exists())

FileUtil.copyFile(srcImgePath, desImagePath);

if (file.exists()) {

//得到文件保存路勁

// 生成縮略圖

String thumbnailsuffix = msgManager1

.getMessage("preview_pic_suffix");

String thumImagePath = desImagePath.replace(".", thumbnailsuffix+".");

PicUtil.createPreviewImage(srcImgePath,thumImagePath);

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

//視頻

if(".mp4".equalsIgnoreCase(suffix))

{

String srcVideoPath = desPath+""+fs.getName();

String desVideopath = "";

File file = new File(srcVideoPath);

try {

String tempurl = msgManager1

.getMessage("video.path.temp");

//videourl :E:\\neweclipsewprkspace\\toponymcensus\\app\\temp\\audio\\

if(file.exists())

FileUtil.copyFile(srcVideoPath, desVideopath);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

//音頻

if(".3gp".equalsIgnoreCase(suffix))

{

String srcAudioPath = srcPath+""+fs.getName();

String desAudioPath = desPath+""+fs.getName();

File file = new File(srcAudioPath);

try {

if(file.exists())

FileUtil.copyFile(srcAudioPath, desAudioPath);

String tempurl = msgManager1

.getMessage("audio.path.temp");//文件的保存路徑

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

/**

* 刪除文件夾下面所有文件及目錄

* @param delpath

*/

public static void deleteFiles(String delpath)

{

try {

File file = new File(delpath);

// 當(dāng)且僅當(dāng)此抽象路徑名表示的文件存在且 是一個(gè)目錄時(shí),返回 true

if (!file.isDirectory())

{

file.delete();

}

else if (file.isDirectory())

{

String[] filelist = file.list();

for (int i = 0; i < filelist.length; i++)

{

File delfile = new File(delpath + "\\" + filelist[i]);

if (!delfile.isDirectory())

{

delfile.delete();

System.out.println(delfile.getAbsolutePath() + "刪除文件成功");

}

else if (delfile.isDirectory())

{

deleteFiles(delpath + "\\" + filelist[i]);

}

}

System.out.println(file.getAbsolutePath()+"刪除成功");

file.delete();

}

} catch (Exception e) {

System.out.println("deletefile() Exception:" + e.getMessage());

}

}

public static void main(String[] args) throws Exception {

//文件解壓

//ZipUtil tz = new ZipUtil();

//tz.unZip("e://fuck.zip", "e://temp//", false);

//文件復(fù)制

String path = "C:\\Users\\Qt\\Desktop\\測試文件";

ZipUtil.moveImageFile(path, "");

//String path = "C:\\Users\\Qt\\Desktop\\測試文件\\11";

//ZipUtil.deleteFiles(path);

}

}

總結(jié)

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

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