码迷,mamicode.com
首页 > 其他好文 > 详细

简易压缩器

时间:2015-06-02 19:55:34      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:

package com.yvdedu;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;

public class b3 extends JFrame implements ActionListener{

File location;
private String position;

b3(){
initFrame();
initComponent();
}

private void initComponent() {

JButton btn1 = new JButton("压缩");
btn1.setFont(new Font("黑体", Font.BOLD, 18));
btn1.setForeground(Color.BLUE);
btn1.addActionListener(this);

JButton btn2 = new JButton("解压");
btn2.setFont(new Font("黑体", Font.BOLD, 18));
btn2.setForeground(Color.GREEN);
btn2.addActionListener(this);

JLabel Label = new JLabel();

ImageIcon icon = new ImageIcon("img/3.gif");
Label.setIcon(icon);

btn1.setPreferredSize(new Dimension(71,100));
btn2.setPreferredSize(new Dimension(71,100));

add(btn1,BorderLayout.WEST);
add(Label,BorderLayout.CENTER);
add(btn2,BorderLayout.EAST);
}

private void initFrame() {
setSize(480,225);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new b3();
}

public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if( cmd.equals("压缩") ){
try {
select("压缩");
} catch (Throwable e1) {
e1.printStackTrace();
}
}
if( cmd.equals("解压") ){
try {
select("解压");
} catch (Throwable e1) {
e1.printStackTrace();
}
}
}

private void select(String str) throws Throwable {
JFileChooser jfc = new JFileChooser();
jfc.setDialogTitle("请选择"+str+"文件");
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//允许选择文件和目录
jfc.setMultiSelectionEnabled(true);//允许选择多个文件
int r = jfc.showOpenDialog(this);
if( r!=JFileChooser.APPROVE_OPTION ){
return;
}
File[] files = jfc.getSelectedFiles();



JFileChooser jfc2= new JFileChooser();
jfc2.setDialogTitle("请选择保存的位置");
jfc2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int r1 = jfc2.showSaveDialog(this);
if( r1!=JFileChooser.APPROVE_OPTION ){
return;
}
location = jfc2.getSelectedFile();
position = location.getAbsolutePath();//保存提示框

File dri = new File("C:/Users/快速启动栏");
ZipTool.zip(new File("Test","a.zip"),dri.getParentFile(), dri);
}
}//第一个文件

 

 

 

 

 

package com.yvdedu;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipTool {
public static void zip(File toZipFile, File pf,File ...files ) throws IOException{
String parentPath = pf.toString()+"\\";
List<File> fileList = each(files);

ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(toZipFile));
byte[] b = new byte[1024*1024];

try{
for( File f:fileList ){
String fileName = f.toString().replace(parentPath, "");
zipEntry( fileName,f,zos,b );
}
}finally{
zos.close();
}
}

private static void zipEntry(String fileName, File file, ZipOutputStream zos,
byte[] b) throws IOException {
FileInputStream fis = new FileInputStream(file);
try{
zos.putNextEntry(new ZipEntry(fileName));
int len;
while( (len=fis.read(b))!=-1 ){
zos.write(b,0,len);
}
zos.closeEntry();
}finally{
fis.close();
}
}

private static List<File> each(File[] files) {
List<File> fileList = new ArrayList<File>();//文件列表
LinkedList<File> tasks = new LinkedList<File>(Arrays.asList(files));//任务列表
while( !tasks.isEmpty() ){
File task = tasks.remove();
if( task.isFile() ){
fileList.add(task);
}
if( task.isDirectory() ){
for( File f:task.listFiles() ){
tasks.add(f);
}
}
}
return fileList;
}//返回File文件列表
}//第二个文件

 

简易压缩器

标签:

原文地址:http://www.cnblogs.com/Mr-xiaowei/p/4547028.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!