java打包,解压

wasaigood 2013-02-05 05:41:59
java实现打包,解压
...全文
119 1 点赞 打赏 收藏 举报
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复


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);
	}
}


自己拿去用吧
  • 打赏
  • 举报
回复
相关推荐
发帖
Java
加入

4.8w+

社区成员

Java相关技术讨论
申请成为版主
帖子事件
创建了帖子
2013-02-05 05:41
社区公告
暂无公告