压缩
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 ;
}
?
?
?