java打包,解压

wasaigood 2013-02-05 05:41:59
java实现打包,解压
...全文
138 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复


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


自己拿去用吧

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧