Java 解压缩助手

我使用Java swing做了一个zip解压缩助手,界面如下:


Java 解压缩助手_第1张图片
使用的依赖包: 

 为什么不用java 自带的ZipUtil呢?因为 没有乱码问题.

技术问题

一,如何使窗口居中?

		Dimension   screensize   =   Toolkit.getDefaultToolkit().getScreenSize();     
	      setSize(600, 280);     
	      Dimension   framesize   =   getSize();     
	      int   x   =   (int)screensize.getWidth()/2   -   (int)framesize.getWidth()/2;     
	      int   y   =   (int)screensize.getHeight()/2   -   (int)framesize.getHeight()/2;     
	      setLocation(x,y);   

 
Java 解压缩助手_第2张图片
 

 

二,增加助记键

效果:按Alt+C 时触发压缩按钮的点击事件,按Alt+D 触发解压按钮的单击事件
Java 解压缩助手_第3张图片
 

 

 三,全局快捷键

/***
	 * 设置全局快捷键,按Alt+r ,则命令输入框自动聚焦
	 */
	private void globalShortcutKeys(){
		//Add global shortcuts
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		// 注册应用程序全局键盘事件, 所有的键盘事件都会被此事件监听器处理.
		toolkit.addAWTEventListener(
				new java.awt.event.AWTEventListener() {
					

					@Override
					public void eventDispatched(AWTEvent event) {
						if (event.getClass() == KeyEvent.class) {
							KeyEvent kE = ((KeyEvent) event);
							// 处理按键事件 Ctrl+Enter
							if ((kE.getKeyCode() == KeyEvent.VK_R)
									&& (((InputEvent) event)
											.isAltDown())&& kE.getID() == KeyEvent.KEY_PRESSED) {
								System.out.println("Alt+r");
								shScriptTF.requestFocus();
							} }
						
					}
				}, java.awt.AWTEvent.KEY_EVENT_MASK);

	}

 
Java 解压缩助手_第4张图片
 

四,解压核心代码

/***
	 * 解压zip
	 * 
	 * @param zipFile
	 * @param decompressLoc
	 *            :解压之后的文件所在目录
	 * @throws ArchiveException
	 * @throws IOException
	 */
	public static boolean deCompressRecursion(String zipFile,
			File decompressLoc, String charSet) throws ArchiveException,
			IOException {
		FileInputStream fin = new FileInputStream(zipFile);
		ArchiveInputStream archIns = new ArchiveStreamFactory()
				.createArchiveInputStream(ArchiveStreamFactory.ZIP, fin);
		ZipArchiveInputStream zipIn = (ZipArchiveInputStream) archIns;
		boolean isSuccess = deCompressRecursion(zipIn, decompressLoc, charSet);
		zipIn.close();
		return isSuccess;
	}
/***
	 * 递归解压缩.
	 * 
	 * @param zipIn
	 * @param decompressLoc
	 * @return
	 * @throws IOException
	 */
	private static boolean deCompressRecursion(ZipArchiveInputStream zipIn,
			File decompressLoc, String charset) throws IOException {
		ZipArchiveEntry zipEntry;
		if (ValueWidget.isNullOrEmpty(charset)) {
			charset = SystemHWUtil.CHARSET_UTF;
		}
		while (!ValueWidget.isNullOrEmpty(zipEntry = zipIn.getNextZipEntry())) {
			byte[] rawName = zipEntry.getRawName();
			String fileName = new String(rawName, charset);
			// System.out.println(fileName);
			if (zipEntry.isDirectory()) {// 是目录
				File newFolder = new File(decompressLoc, fileName);// 若子目录不存在,则创建之
				if(isPrint)
				System.out.println(newFolder.getAbsolutePath());
				if (!newFolder.exists()) {
					newFolder.mkdir();
				}
				// deCompressRecursion(zipIn, decompressLoc,charset);
			} else {// 是普通文件
				File singFile = new File(decompressLoc, fileName);
				if(isPrint)
				System.out.println(singFile.getAbsolutePath());
				if (singFile.exists()) {// 若解压后的文件已经存在,则直接退出
					GUIUtil23.warningDialog("File \""
							+ singFile.getAbsolutePath() + "\" does  exist.");
					return false;
				}
				/**
				 * 以下四行代码是后来添加的,为了解决父目录不存在的问题
				 */
				File fatherFolder = singFile.getParentFile();
				if (!fatherFolder.exists()) {
					fatherFolder.mkdirs();
				}
				FileUtils.writeIn2Output(zipIn, new FileOutputStream(singFile),
						true, false);
			}
		}
		return true;
	}

 

五,压缩核心代码

/***
	 * 压缩文件.
	 * 
	 * @param zipFile
	 * @param folderPaths
	 * @return
	 * @throws ArchiveException
	 * @throws IOException
	 */
	public static boolean compressZipRecursion(String zipFile,
			String folderPaths) throws ArchiveException, IOException {
		FileOutputStream fou = new FileOutputStream(zipFile);
		ArchiveOutputStream archOuts = new ArchiveStreamFactory()
				.createArchiveOutputStream(ArchiveStreamFactory.ZIP, fou);
		if (archOuts instanceof ZipArchiveOutputStream) {
			ZipArchiveOutputStream zipOut = (ZipArchiveOutputStream) archOuts;
			List<ZipArchiveEntry> zipEntrys = getZipFileListRecursion(new File(
					folderPaths), null);
			for (int i = 0; i < zipEntrys.size(); i++) {
				ZipArchiveEntry zipEntry2 = zipEntrys.get(i);
				zipOut.putArchiveEntry(zipEntry2);
				File file = new File(folderPaths, zipEntry2.getName());
				if (!file.exists()) {
					return false;
				}
				if (!file.isDirectory()) {
					FileInputStream fin = new FileInputStream(file);
					// 不要关闭zipOut,关闭之前要执行closeArchiveEntry()
					FileUtils.writeIn2Output(fin, zipOut, false, true);
				}
			}
			closeZip(zipOut, true);

		}
		return true;
	}

 

源码zip_mgmt.zip及依赖的jar包见附件

参考:http://hw1287789687.iteye.com/blog/2050132

你可能感兴趣的:(压缩,zip,解压,java解压缩,ZipArchiveEntry)