檔案加壓工具類?

Tags: 作者, 檔案, 工具, word,

package com.sasgas.ldws.util;

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.List;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;

public class JZip {

public static int iCompressLevel;// 壓縮比 取值範圍為0~9

public static boolean bOverWrite;// 是否覆蓋同名檔案 取值範圍為True和False

private static List filelist = new ArrayList ();

public static String sErrorMessage;

private String zipFilePath;

public List srcMap;

public JZip() {

iCompressLevel = 9;

}

public JZip(String zipFilePath) throws FileNotFoundException, IOException {

this.zipFilePath = zipFilePath;

}

@SuppressWarnings("unchecked")

public static ArrayList extract(String sZipPathFile, String sDestPath) {

ArrayList AllFileName = new ArrayList();

try {

// 先指定壓縮檔的位置和檔名,建立FileInputStream物件

FileInputStream fins = new FileInputStream(sZipPathFile);

// 將fins傳入ZipInputStream中

ZipInputStream zins = new ZipInputStream(fins);

ZipEntry ze = null;

byte ch[] = new byte[256];

while ((ze = zins.getNextEntry()) != null) {

File zfile = new File(sDestPath + ze.getName());

File fpath = new File(zfile.getParentFile().getPath());

if (ze.isDirectory()) {

if (!zfile.exists())

zfile.mkdirs();

zins.closeEntry();

} else {

if (!fpath.exists())

fpath.mkdirs();

FileOutputStream fouts = new FileOutputStream(zfile);

int i;

AllFileName.add(zfile.getAbsolutePath());

while ((i = zins.read(ch)) != -1)

fouts.write(ch, 0, i);

zins.closeEntry();

fouts.close();

}

}

fins.close();

zins.close();

sErrorMessage = "OK";

} catch (Exception e) {

System.err.println("Extract error:" + e.getMessage());

sErrorMessage = e.getMessage();

}

AllFileName.clear();

return AllFileName;

}

@SuppressWarnings( { "unchecked", "static-access" })

public static void compress(String sPathFile, boolean bIsPath,

String sZipPathFile) {

BufferedReader in = null;

FileOutputStream fos = null;

org.apache.tools.zip.ZipOutputStream zos = null;

org.apache.tools.zip.ZipEntry entry = null;

File file = null;

String sPath = "";

if (filelist != null) {

filelist.clear();

}

if (bIsPath == true) {

searchFiles(sPathFile);

sPath = sPathFile;

} else {

File myfile = new File(sPathFile);

sPath = sPathFile.substring(0, sPathFile

.lastIndexOf(myfile.separator) + 1);

filelist.add(myfile.getPath());

}

try {

fos = new FileOutputStream(sZipPathFile);

file = new File(sZipPathFile);

zos = new org.apache.tools.zip.ZipOutputStream(fos);

int len = 0;

for (String fileFullName : filelist) {

in = new BufferedReader(new InputStreamReader(

new FileInputStream(fileFullName), "iso-8859-1"));

// 壓縮到zip檔案中

entry = new org.apache.tools.zip.ZipEntry(fileFullName

.substring((sPath).length()));

zos.putNextEntry(entry);

while ((len = in.read()) != -1) {

zos.write(len);

}

zos.closeEntry();

in.close();

}

} catch (IOException e) {

e.printStackTrace();

if (file.exists()) {

file.delete();

}

} finally {

if (null != zos) {

try {

zos.close();

} catch (IOException e) {

e.printStackTrace();

}

zos = null;

}

if (null != fos) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

fos = null;

}

}

}

/*

* 這是一個遞迴過程,功能是檢索出所有的檔名稱 dirstr:目錄名稱

*/

@SuppressWarnings("unchecked")

private static void searchFiles(String dirstr) {

File tempdir = new File(dirstr);

if (tempdir.exists()) {

if (tempdir.isDirectory()) {

File[] tempfiles = tempdir.listFiles();

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

if (tempfiles[i].isDirectory())

searchFiles(tempfiles[i].getPath());

else {

filelist.add(tempfiles[i].getPath());

}

}

} else {

filelist.add(tempdir.getPath());

}

}

}

public String getZipFilePath() {

return zipFilePath;

}

public void setZipFilePath(String zipFilePath) {

this.zipFilePath = zipFilePath;

}

/**

* 解析zip檔案得到檔名

*

* @return

* @throws FileNotFoundException

* @throws IOException

*/

public boolean parserZip() throws FileNotFoundException, IOException {

FileInputStream fis = new FileInputStream(zipFilePath);

ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));

ZipEntry entry;

try {

srcMap = new ArrayList ();

while ((entry = zis.getNextEntry()) != null) {

File file = new File(zipFilePath + File.separator

+ entry.getName());

srcMap.add(file);

}

zis.close();

fis.close();

return true;

} catch (IOException e) {

return false;

}

}

/**

*

* @param zipFileName

* 待解壓縮的ZIP檔案

* @param extPlace

* 解壓後的資料夾

*/

public static void extZipFileList(String zipFileName, String extPlace) {

try {

ZipInputStream in = new ZipInputStream(new FileInputStream(

zipFileName));

File files = new File(extPlace);

files.mkdirs();

ZipEntry entry = null;

while ((entry = in.getNextEntry()) != null) {

String entryName = entry.getName();

if (entry.isDirectory()) {

File file = new File(files + "/" + entryName);

file.mkdirs();

System.out.println("建立資料夾:" + entryName);

} else {

OutputStream os = new FileOutputStream(files

+ File.separator + entryName);

// Transfer bytes from the ZIP file to the output file

byte[] buf = new byte[1024];

int len;

while ((len = in.read(buf)) > 0) {

os.write(buf, 0, len);

}

os.close();

in.closeEntry();

System.out.println("解壓檔案:" + entryName);

}

}

} catch (IOException e) {

}

}

public static void main(String args[]) {

System.out.println("start..");

JZip.compress("c:/su", true, "c:/su.zip");

System.out.println("end..");

}

}

作者, 檔案, 工具, word,
相關問題答案