首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

java封装,解压

2013-02-24 
java打包,解压java实现打包,解压[解决办法]import java.io.BufferedInputStreamimport java.io.BufferedO

java打包,解压
java实现打包,解压
[解决办法]



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.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * Zip工具类
 */
public class ZipUtil {
private static final String PATH_SEP = "\";
public static final int BUFFER = 2048;

/**
 * 解压缩zip文件
 * 
 * @param zipFileName
 *            zip文件路径
 * @param fileExtractPath
 *            解压缩目录
 * @param projectName
 *            工程名
 * @throws IOException
 */
public static File unzipFilesToPath(String zipFileName,
String fileExtractPath, String projectName) {
//解压前 先删除目录中存在的目录
if(projectName != null && !"".equals(projectName)){
File dir = new File(fileExtractPath + PATH_SEP + projectName);
if(dir.exists()){
deleteFile(dir);
}
}

FileInputStream fis = null;
ZipInputStream zis = null;
try {
fis = new FileInputStream(zipFileName);
zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
File resultFile =null;
int i=0;
while ((entry = zis.getNextEntry()) != null) {
int count;
byte[] data = new byte[BUFFER];


String fopath = fileExtractPath + PATH_SEP + projectName
+ PATH_SEP + entry.getName();
if (entry.isDirectory()) {
if(i==0){
resultFile = new File(fileExtractPath + PATH_SEP + projectName
+ PATH_SEP + entry.getName());
}
i++;
new File(fopath).mkdirs();
continue;
}
final FileOutputStream fos = new FileOutputStream(fopath);
final BufferedOutputStream dest = new BufferedOutputStream(fos,
BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
i++;
}
return resultFile;

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
zis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}


@SuppressWarnings("resource")
public static File unzipFilesToPathThrowException(String zipFileName,
String fileExtractPath, String projectName) throws IOException {


//解压前 先删除目录中存在的目录
if(projectName != null && !"".equals(projectName)){
File dir = new File(fileExtractPath + PATH_SEP + projectName);
if(dir.exists()){
deleteFile(dir);
}
}

FileInputStream fis = null;
ZipInputStream zis = null;
try {
fis = new FileInputStream(zipFileName);
zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
File resultFile =null;
int i=0;
while ((entry = zis.getNextEntry()) != null) {
int count;
byte[] data = new byte[BUFFER];


String fopath = fileExtractPath + PATH_SEP + projectName
+ PATH_SEP + entry.getName();
if (entry.isDirectory()) {
if(i==0){
resultFile = new File(fileExtractPath + PATH_SEP + projectName
+ PATH_SEP + entry.getName());
}
i++;
new File(fopath).mkdirs();
continue;
}
final FileOutputStream fos = new FileOutputStream(fopath);
final BufferedOutputStream dest = new BufferedOutputStream(fos,
BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
i++;
}
return resultFile;

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
throw new IOException();
} finally {
try {
fis.close();
zis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

/**
 * 加压缩zip文件
 * 
 * @param zipFileName
 *            zip文件路径
 * @param fileExtractPath
 *            解压缩目录
 * @param projectName
 *            工程名
 * @throws IOException
 */
public static void unzipFilesToPath(InputStream inputStream,
String fileExtractPath, String projectName) {
ZipInputStream zis = null;
try {
zis = new ZipInputStream(inputStream);
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
int count;
byte[] data = new byte[BUFFER];
String fopath = fileExtractPath + PATH_SEP + projectName
+ PATH_SEP + entry.getName();
if (entry.isDirectory()) {
new File(fopath).mkdirs();
continue;
}
final FileOutputStream fos = new FileOutputStream(fopath);
final BufferedOutputStream dest = new BufferedOutputStream(fos,
BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}

} catch (Exception e) {


e.printStackTrace();
} finally {
try {
zis.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

public static boolean deleteFile(File file)
{
if (file == null 
[解决办法]
 !file.exists()){
return false;
}
if (file.isFile()){
file.delete();
}else{
File[] fileList = file.listFiles();

for (int i = 0; i < fileList.length; i++)
{
deleteFile(fileList[i]);
}
file.delete();
}
return true;
}

/**
 * 获取压缩文件第一个目录
 * 
 * @param zipFileName
 *            zip文件路径
 * @param fileExtractPath
 *            解压缩目录
 * @param projectName
 *            工程名
 * @throws IOException
 */
public static String getUnzipFileFirstDir(String zipFileName) {

FileInputStream fis = null;
ZipInputStream zis = null;
try {
fis = new FileInputStream(zipFileName);
zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
String firstDir;
int i=0;
while ((entry = zis.getNextEntry()) != null) {
if (entry.isDirectory()) {
if(i==0){
firstDir = PATH_SEP + entry.getName();
i++;
return firstDir;
}

}
}

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
zis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

public static void main(String[] args) {
//ZipUtil.unzipFilesToPath("d:\\我.zip","d:\\我2","我2test");
 String string = ZipUtil.getUnzipFileFirstDir("d:\\html5.zip");
 System.out.println(string);
}
}


自己拿去用吧

热点排行