📄 ziputil.java
字号:
/*******************************************************************************
* $Header: /cvsroot/EOS6/work_dir/niegy/com.primeton.studio.gef.ui/src/com/primeton/studio/gef/ui/zip/ZipUtil.java,v 1.1 2007/01/23 06:35:15 niegy Exp $
* $Revision: 1.1 $
* $Date: 2007/01/23 06:35:15 $
*
*==============================================================================
*
* Copyright (c) 2001-2006 Primeton Technologies, Ltd.
* All rights reserved.
*
* Created on 2007-1-22
*******************************************************************************/
package com.primeton.studio.gef.ui.zip;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
/**
* for example:
* try{
* String targetZip = "C:\\Downloads\\WGA\\test.rar";
* String sourceFolder= "C:\\Downloads\\WGA\\WGA";
* ZipUtil.zipFile(targetZip, sourceFolder);
*
* String strZip = targetZip;
* String strFolder = "C:\\Downloads\\WGA\\1";
* String strFolder2 = "C:\\Downloads\\WGA\\2";
* Map<String, InputStream> map = ZipUtil.getZipFiles(strZip);
* for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
* String fileName = (String) iter.next();
* InputStream input = map.get(fileName);
* String filePath = strFolder2 + "/" +fileName;
* BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(filePath));
* int c;
* while ( (c = input.read()) != -1){
* out.write(c);
* }
* out.close();
* input.close();
* }
* ZipUtil.unzipFile(strZip, strFolder);
* }
* catch(Exception e){
* e.printStackTrace();
* }
*
* @author niegy (mailto:niegy@primeton.com)
*/
/*
* 修改历史
* $Log: ZipUtil.java,v $
* Revision 1.1 2007/01/23 06:35:15 niegy
* 实现压缩功能,支持中文
*
*/
public class ZipUtil {
public static void zipFile(String targetZip, String[] sourceFolders, String[] prefixs, IProgressMonitor monitor)throws Exception{
File targetFile = new File(targetZip);
if(targetFile.exists()){
targetFile.delete();
}
//定义输入的压缩文件
CZipOutputStream out = null;
out = new CZipOutputStream(new BufferedOutputStream(new FileOutputStream(targetZip)),"GB2312");
try {
monitor.beginTask("", sourceFolders.length);
for(int index = 0; index < sourceFolders.length; index++) {
String sourceFolder = sourceFolders[index];
String prefix = prefixs[index];
//有效性检查
File fileFolder = new File(sourceFolder);
if (!fileFolder.exists()){
throw new IllegalArgumentException("待压缩的文件不存在:" + sourceFolder);
}
sourceFolder = getStandardPath(sourceFolder);
monitor.worked(1);
//将文件夹下的文件一一读出
List listFile = getChildFileByFolder(fileFolder);
SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, 1);
subMonitor.beginTask("", listFile.size());
for (int i = 0; i < listFile.size(); i++){
File file = (File) listFile.get(i);
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
int c;
String strFile = getStandardPath(file.toString());
//取得文件相对于待压缩文件的相对路径
String strRelativePath = strFile.substring(strFile.indexOf(sourceFolder) + sourceFolder.length() + 1, strFile.length());
// System.out.println("strRelativePath=" + strRelativePath);
if (prefix != null) {
String fileSeparator = System.getProperty("file.separator"); //$NON-NLS-1$
strRelativePath = prefix + fileSeparator + strRelativePath;
}
subMonitor.setTaskName("压缩文件 " + strRelativePath);
out.putNextEntry(new CZipEntry(strRelativePath));
while ( (c = in.read()) != -1){
out.write(c);
}
in.close();
subMonitor.worked(1);
}
}
} finally {
monitor.done();
if(out != null)
out.close();
}
}
public static void zipFile(String targetZip, String[] sourceFolders, String[] prefixs)throws Exception{
zipFile(targetZip, sourceFolders, prefixs, new NullProgressMonitor());
}
/**
* 将 sourceFolder 中的文件压缩到 zip 文件targetZip中, 空目录将被忽略
* @param targetZip
* @param sourceFolder
* @throws Exception
*/
public static void zipFile(String targetZip, String sourceFolder, String prefix)throws Exception{
zipFile(targetZip, new String[] {sourceFolder}, new String[] {prefix});
}
/**
* 将 sourceFolder 中的文件压缩到 zip 文件targetZip中
* @param targetZip
* @param sourceFolder
* @throws Exception
*/
public static void zipFile(String targetZip, String sourceFolder)throws Exception{
zipFile(targetZip, sourceFolder, null);
}
/**
* 使用递归取得文件夹下的所有文件, 不包括目录
* @param fileFolder 文件夹名
* @return 文件对象组成的List
*/
private static List getChildFileByFolder(File fileFolder){
List listFile = new ArrayList();
if (fileFolder.isDirectory()){
File[] files = fileFolder.listFiles();
for (int i = 0; i < files.length; i++){
listFile.addAll(getChildFileByFolder(files[i]));
}
} else{
listFile.add(fileFolder);
}
return listFile;
}
/**
* 作用:取得分隔号置为"/"的path
*
* @param strPath
* @return 路径名
*/
private static String getStandardPath(String strPath) {
if (strPath == null) {
return null;
}
return strPath.replace('\\', '/');
}
public static void unzipFile(String strZip, String strFolder, IProgressMonitor monitor)throws Exception{
//有效性检查
File fileZip = new File(strZip);
if (!fileZip.exists()){
throw new IllegalArgumentException("待解压的文件不存在:" + strZip);
}
//建立目标文件夹
File fileFolder = new File(strFolder);
if(!fileFolder.exists()){
fileFolder.mkdirs();
}
CZipInputStream zipInput = new CZipInputStream(new BufferedInputStream(new FileInputStream(fileZip)),"GB2312");
//读入压缩文件
InputStream in = null;
try{
CZipEntry zipEntry;
int entriesCount = 0;
while(zipInput.getNextEntry() != null)
entriesCount++;
zipInput.close();
zipInput = new CZipInputStream(new BufferedInputStream(new FileInputStream(fileZip)),"GB2312");
monitor.beginTask("", entriesCount);
while((zipEntry = zipInput.getNextEntry()) != null) {
String strUnzipFile = strFolder + "/" + zipEntry.getName();
File fileUnzipFile = new File(strUnzipFile);
File fileUnzipFolder = new File(fileUnzipFile.getParent());
fileUnzipFolder.mkdirs();
//解压文件
monitor.setTaskName("解压文件 " + zipEntry.getName());
monitor.worked(1);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(strUnzipFile));
int c;
while ( (c = zipInput.read()) != -1){
out.write(c);
}
out.close();
}
monitor.done();
}finally{
if(in != null) in.close();
if(zipInput != null) zipInput.close();
}
}
/**
* 解压文件
* @param zip 待解压的zip文件名
* @param folder 解压到的文件夹
*/
public static void unzipFile(String strZip, String strFolder)throws Exception{
unzipFile(strZip, strFolder, new NullProgressMonitor());
}
/**
* 返回解压的zip文件名中文件名和InputStream的内容
* @param strZip 待解压的zip文件名
* @return
* @throws Exception
*/
public static Map<String, InputStream>getZipFiles(String strZip) throws Exception{
Map<String, InputStream> result = new HashMap<String, InputStream>();
//有效性检查
File fileZip = new File(strZip);
if (!fileZip.exists()){
throw new IllegalArgumentException("待解压的文件不存在:" + strZip);
}
CZipInputStream zipInput = new CZipInputStream(new BufferedInputStream(new FileInputStream(fileZip)),"GB2312");
try{
CZipEntry zipEntry;
while((zipEntry = zipInput.getNextEntry()) != null) {
CZipInputStream zipInput1 = new CZipInputStream(new BufferedInputStream(new FileInputStream(fileZip)),"GB2312");
CZipEntry zipEntry1;
while((zipEntry1 = zipInput1.getNextEntry()) != null) {
if(zipEntry1.getName().equals(zipEntry.getName())){
result.put(zipEntry1.getName(), zipInput1);
break;
}
}
}
}finally{
if(zipInput != null) zipInput.close();
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -