- 浏览: 14779 次
- 性别:
- 来自: 成都
最近访客 更多访客>>
文章分类
最新评论
-
foolishdault:
这个对中文支持不够好,还是使用apache的实现比较好
java以zip格式实现压缩解压,有界面
压缩
package Util; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.CRC32; import java.util.zip.CheckedOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipUtils { public static final String EXT = ".zip"; private static final String BASE_DIR = ""; // 符号"/"用来作为目录标识判断符 private static final String PATH = "/"; private static final int BUFFER = 1024; /** * 压缩 * @param srcFile * @throws Exception */ public static void compress(File srcFile) throws Exception { String name = srcFile.getName(); String basePath = "D:\\" ; String destPath = basePath + name + EXT; compress(srcFile, destPath); } /** * 压缩 * * @param srcFile * 源路径 * @param destPath * 目标路径 * @throws Exception */ public static void compress(File srcFile, File destFile) throws Exception { // 对输出文件做CRC32校验 CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream( destFile), new CRC32()); ZipOutputStream zos = new ZipOutputStream(cos); compress(srcFile, zos, BASE_DIR); zos.flush(); zos.close(); } /** * 压缩文件 * * @param srcFile * @param destPath * @throws Exception */ public static void compress(File srcFile, String destPath) throws Exception { compress(srcFile, new File(destPath)); } /** * 压缩 * * @param srcFile * 源路径 * @param zos * ZipOutputStream * @param basePath * 压缩包内相对路径 * @throws Exception */ private static void compress(File srcFile, ZipOutputStream zos, String basePath) throws Exception { if (srcFile.isDirectory()) { compressDir(srcFile, zos, basePath); } else { compressFile(srcFile, zos, basePath); } } /** * 压缩 * * @param srcPath * @throws Exception */ public static void compress(String srcPath) throws Exception { File srcFile = new File(srcPath); compress(srcFile); } /** * 文件压缩 * * @param srcPath * 源文件路径 * @param destPath * 目标文件路径 * */ public static void compress(String srcPath, String destPath) throws Exception { File srcFile = new File(srcPath); compress(srcFile, destPath); } /** * 压缩目录 * * @param dir * @param zos * @param basePath * @throws Exception */ private static void compressDir(File dir, ZipOutputStream zos, String basePath) throws Exception { File[] files = dir.listFiles(); // 构建空目录 if (files.length < 1) { ZipEntry entry = new ZipEntry(basePath + dir.getName() + PATH); zos.putNextEntry(entry); zos.closeEntry(); } for (File file : files) { // 递归压缩 compress(file, zos, basePath + dir.getName() + PATH); } } /** * 文件压缩 * * @param file * 待压缩文件 * @param zos * ZipOutputStream * @param dir * 压缩文件中的当前路径 * @throws Exception */ private static void compressFile(File file, ZipOutputStream zos, String dir) throws Exception { /** * 压缩包内文件名定义 * * <pre> * 如果有多级目录,那么这里就需要给出包含目录的文件名 * 如果用WinRAR打开压缩包,中文名将显示为乱码 * </pre> */ ZipEntry entry = new ZipEntry(dir + file.getName()); zos.putNextEntry(entry); BufferedInputStream bis = new BufferedInputStream(new FileInputStream( file)); int count; byte data[] = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { zos.write(data, 0, count); } bis.close(); zos.closeEntry(); } }
解压工具类
package Util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class UnZipUtils { public static void main(String[] args) throws IOException { unzip("D:\\test\\test.zip"); } //-----------------------------------------方法一------------------------------------------------------------ /** * 采用 <code>ZipInputStream<code>读Zip文件的方式 * 解压到当前目录 * @param zipFilePath 需要解压的文件的路径 * @throws IOException */ public static void unzip(String zipFilePath) throws IOException { File zipFile = new File(zipFilePath); String zipFileName = zipFile.getName(); String rootFileName = zipFile.getParentFile().getPath() + "/" + zipFileName.substring(0, zipFileName.lastIndexOf(".")); unzip(zipFilePath, rootFileName); } /** * 采用 <code>ZipInputStream<code>读Zip文件的方式 * 解压到指定目录 * @param zipFilePath 需要解压的文件的路径 * @param unzipFilePath 解压后文件的路径 * @throws IOException */ public static void unzip(String zipFilePath, String unzipFilePath) throws IOException { File rootFile = new File(unzipFilePath); rootFile.mkdirs(); ZipInputStream input = new ZipInputStream( new BufferedInputStream(new FileInputStream(zipFilePath))); ZipEntry entry = null; //Zip文件将里面的每个文件都作为一个ZipEntry, 父目录和子文件为两个单独的entry while((entry = input.getNextEntry()) != null) { File tmpFile = new File(unzipFilePath + "/" + entry.getName()); tmpFile.getParentFile().mkdirs(); if(entry.isDirectory()) { tmpFile.mkdir(); } else { BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(tmpFile)); byte[] datas = new byte[2048]; int count; while((count = input.read(datas)) != -1) { output.write(datas, 0, count); } output.close(); } input.closeEntry(); } input.close(); }}
界面类
package main; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import java.util.List; import java.util.zip.*; import javax.swing.*; import Util.UnZipUtils; import Util.ZipUtils; /** * @version 1.00 2011-11-20 * @author Wang Chuanheng */ public class ZipTest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { ZipTestFrame frame = new ZipTestFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } } /** * A frame with a text area to show the contents of a file inside a ZIP archive, a combo box to * select different files in the archive, and a menu to load a new archive. */ class ZipTestFrame extends JFrame { public ZipTestFrame() { setTitle("不支持中文解压!author: 王传恒2009110336"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // add the menu and the Open and Exit menu items JMenuBar menuBar = new JMenuBar(); JMenu menu1 = new JMenu("解压") ; JMenu menu2 = new JMenu("压缩") ; JMenuItem openItem = new JMenuItem("选择一个zip文件"); menu1.add(openItem); openItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(".")); int r = chooser.showOpenDialog(ZipTestFrame.this); if (r == JFileChooser.APPROVE_OPTION) { zipname = chooser.getSelectedFile().getPath(); scanZipFile(); } } }); //开始解压 JMenuItem jieyaItem = new JMenuItem("开始解压"); menu1.add(jieyaItem); jieyaItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { try { model.addElement("开始解压到文件所在目录") ; UnZipUtils.unzip(zipname) ; model.addElement("解压成功!") ; } catch (IOException e) { model.addElement("解压error,不支持中文") ; e.printStackTrace(); } } }); // 添加 JMenuItem addItem = new JMenuItem("添加要压缩的文件或文件夹"); menu2.add(addItem); addItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { //addFile() ; JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES) ; chooser.setCurrentDirectory(new File("D://")); int r = chooser.showOpenDialog(ZipTestFrame.this); if (r == JFileChooser.APPROVE_OPTION) { addFilename = chooser.getSelectedFile().getPath(); addFile() ; System.out.println(addFilename) ; } } }); // 压缩选定文件 JMenuItem zipItem = new JMenuItem("开始压缩"); menu2.add(zipItem); zipItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { try { ZipUtils.compress(addFilename) ; model.addElement("压缩成功!") ; } catch (Exception e) { model.addElement("压缩error,不支持中文") ; //System.out.println("压缩error!") ; e.printStackTrace(); } } }); // exit JMenuItem exitItem = new JMenuItem("退出"); menu1.add(exitItem); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); menuBar.add(menu1); menuBar.add(menu2) ; setJMenuBar(menuBar); // add the text area and combo box model = new DefaultListModel() ; model.addElement("文件列表") ; fileList = new JList(model); //fileList.setVisibleRowCount(12) ; add(new JScrollPane(fileList), BorderLayout.CENTER); } /** * Scans the contents of the ZIP archive and populates the combo box. */ public void scanZipFile() { model.removeAllElements() ; new SwingWorker<Void, String>() { protected Void doInBackground() throws Exception { ZipInputStream zin = new ZipInputStream(new FileInputStream(zipname)); ZipEntry entry; while ((entry = zin.getNextEntry()) != null) { publish(entry.getName()); zin.closeEntry(); } zin.close(); return null; } protected void process(List<String> names) { for (String name : names) model.addElement(name) ; } }.execute(); } public void addFile(){ System.out.println("addFile()") ; model.removeAllElements() ; model.addElement("准备压缩: "+addFilename) ; model.addElement("到 D:\\ ") ; } public static final int DEFAULT_WIDTH = 800; public static final int DEFAULT_HEIGHT = 600; private JList fileList; private String zipname; private String addFilename ; DefaultListModel model ; }
相关推荐
通过设计,允许创建ZIP压缩文件,并对ZIP压缩文件中包含的文件进行显示、添加、解压、删除等操作。GUI界面与下图类似: 【实验目的】 要求学生能熟练使用基于Swing的GUI设计,熟练使用常用组件和容器,理解java事件...
java版本的zip压缩和解压程序,命令行的,没有界面,支持文件夹压缩解压
编写一个Java图形用户界面程序,可以打开一个对话框选择1个或多个文件进行压缩,压缩后的文件为ZIP格式,也可以选择ZIP压缩文件将其解压。在压缩文件时可以选择将文件分割成几份压缩包文件,同样解压缩时可以选择多...
但WinRAR就不同了,不但能解压多数压缩格式,且不需外挂程序支持就可直接建立 ZIP 格式的压缩文件,所以我们不必担心离开了其他软件如何处理 ZIP 格式的问题。 4、设置项目非常完善,并且可以定制界面 通过...
内容聚焦于Java文件压缩与解压缩功能的实现,采用高效、便捷的算法,支持多种常见的文件格式。项目结构清晰,注释充分,易于理解和二次开发。 主要功能特点: 1. 支持主流压缩格式:如ZIP、RAR、7z等。 2. 文件...
本系统将基于JAVA语言开发,使用JAVA自带的Zip文件压缩与解压缩类库,同时结合JAVA的加密解密类库,实现文件压缩与解压缩以及加密解密的功能。 ## 预期成果 本毕业设计旨在实现一个基于JAVA文件压缩与解压缩系统,...
Java用Zip压缩多个文件实例源码.rar Java用的在线地图浏览模块.rar Java约瑟夫环演示Applet源码.rar java网络五子棋的源代码.rar JAVA网络抓包程序.rar Java转换xml.rar java项目源码在线相册系统.rar 书籍管理系统....
Java zip压缩包查看程序,应用弹出文件选择框,选择ZIP格式的压缩文件,可以像Winrar软件一样查看压缩文件内部的文件及文件夹,源码截图如上所示。 Java 数字签名、数字证书生成源码 2个目标文件 摘要:JAVA源码,...
Java zip压缩包查看程序,应用弹出文件选择框,选择ZIP格式的压缩文件,可以像Winrar软件一样查看压缩文件内部的文件及文件夹,源码截图如上所示。 Java 数字签名、数字证书生成源码 2个目标文件 摘要:JAVA源码,...
Java zip压缩包查看程序,应用弹出文件选择框,选择ZIP格式的压缩文件,可以像Winrar软件一样查看压缩文件内部的文件及文件夹,源码截图如上所示。 Java 数字签名、数字证书生成源码 2个目标文件 摘要:JAVA源码,...
Java zip压缩包查看程序,应用弹出文件选择框,选择ZIP格式的压缩文件,可以像Winrar软件一样查看压缩文件内部的文件及文件夹,源码截图如上所示。 Java 数字签名、数字证书生成源码 2个目标文件 摘要:JAVA源码...
Java zip压缩包查看程序,应用弹出文件选择框,选择ZIP格式的压缩文件,可以像Winrar软件一样查看压缩文件内部的文件及文件夹,源码截图如上所示。 Java 数字签名、数字证书生成源码 2个目标文件 摘要:JAVA源码...
Java zip压缩包查看程序,应用弹出文件选择框,选择ZIP格式的压缩文件,可以像Winrar软件一样查看压缩文件内部的文件及文件夹,源码截图如上所示。 Java 数字签名、数字证书生成源码 2个目标文件 摘要:JAVA源码...
Java zip压缩包查看程序源码 1个目标文件 摘要:Java源码,文件操作,压缩包查看 Java zip压缩包查看程序,应用弹出文件选择框,选择ZIP格式的压缩文件,可以像Winrar软件一样查看压缩文件内部的文件及文件夹,源码...
目前,微信小程序已经覆盖了电商购物、生活服务、娱乐休闲、教育学习、工具助手等多个领域,为数以亿计的用户提供便捷的服务入口,也为众多商家和开发者提供了新的商业模式和创业机会。随着技术的不断升级和完善,小...
Java zip压缩包查看程序源码 1个目标文件 摘要:Java源码,文件操作,压缩包查看 Java zip压缩包查看程序,应用弹出文件选择框,选择ZIP格式的压缩文件,可以像Winrar软件一样查看压缩文件内部的文件及文件夹,源码...
运用面向对象程序设计思想,基于Java文件管理和I/O框架,实现基于图形界面的GUI文件管理器。 1、实现文件夹创建、删除、进入。 2、实现当前文件夹下的内容罗列。 3、实现文件拷贝和文件夹拷贝(文件夹拷贝指深度...
1、实现文件夹创建、删除、进入。 2、实现当前文件夹下的内容罗列。 3、实现文件拷贝和文件夹拷贝(文件夹拷贝指深度拷贝,包括所有子目录和文件)。...6、实现压缩文件的解压。 7、文件管理器具有图形界面。
1、实现文件夹创建、删除、进入。 2、实现当前文件夹下的内容罗列。 3、实现文件拷贝和文件夹拷贝(文件夹拷贝指深度拷贝,包括所有...6、实现压缩文件的解压。 7、文件管理器文件树。 8、文件管理器具有图形界面。