GZip压缩解压
来源:互联网 发布:notepad格式化json 编辑:程序博客网 时间:2024/05/29 18:52
import java.awt.Label;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.zip.GZIPInputStream;import java.util.zip.GZIPOutputStream;import javax.swing.*;public class GZipDemo extends JFrame{JFileChooser fileChooser;JTextField source;JTextField target;JButton select1;JButton select2;JButton jbZip;JButton jbUnZip;public GZipDemo(){super("用GZip压缩解压文件");fileChooser=new JFileChooser();source=new JTextField(16);target=new JTextField(16);select1=new JButton("选择");select2=new JButton("选择");jbZip =new JButton("压缩");jbUnZip=new JButton("解压");Box box=Box.createVerticalBox();JPanel panel=new JPanel();panel.add(new Label("源文件"));panel.add(source);panel.add(select1);box.add(panel);panel=new JPanel();panel.add(new JLabel("目标文件"));panel.add(target);panel.add(select2);box.add(panel);panel=new JPanel();panel.add(jbZip);panel.add(jbUnZip);box.add(panel);getContentPane().add(box);jbZip.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubzipFile(source.getText(),target.getText());}});jbUnZip.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubunZipFile(source.getText(),target.getText());}});select1.addActionListener(new SelectListener());select2.addActionListener(new SelectListener());setSize(330,150);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public void zipFile(String source,String target){try{FileInputStream in=new FileInputStream(source);FileOutputStream out=new FileOutputStream(target);GZIPOutputStream gzout=new GZIPOutputStream(out);byte[] buf=new byte[1024];int num;while((num=in.read(buf))!=-1){gzout.write(buf,0,num);}gzout.close();out.close();in.close();JOptionPane.showMessageDialog(GZipDemo.this, "压缩成功");}catch(Exception e){e.printStackTrace();JOptionPane.showMessageDialog(GZipDemo.this,"解压失败");}}public void unZipFile(String source,String target){try{FileInputStream in=new FileInputStream(source);FileOutputStream out=new FileOutputStream(target);GZIPInputStream gzin=new GZIPInputStream(in);byte[] buf=new byte[1024];int num;while((num=gzin.read(buf,0,buf.length))!=-1){out.write(buf,0,num);}gzin.close();out.close();in.close();JOptionPane.showMessageDialog(GZipDemo.this,"解压成功");}catch(Exception e){e.printStackTrace();JOptionPane.showMessageDialog(GZipDemo.this,"解压失败");}}class SelectListener implements ActionListener{public void actionPerformed(ActionEvent e){if(fileChooser.showOpenDialog(GZipDemo.this)==JFileChooser.APPROVE_OPTION){String fileName=fileChooser.getSelectedFile().getAbsolutePath();if(e.getSource().equals(select1)){source.setText(fileName);}else{target.setText(fileName);}}}}public static void main(String[] args){new GZipDemo();}}
0 0
- gzip解压和压缩
- Gzip压缩和解压
- GZIP压缩解压类
- GZIP压缩和解压
- Gzip解压和压缩
- Gzip压缩数据解压
- GZIP压缩解压类
- GZip压缩解压
- GZip 压缩解压 --- Pako 的压缩解压
- J2ME gzip压缩与解压
- unix tar gzip压缩解压
- GZIP简单压缩与解压
- iOS gzip 压缩和解压
- php解压gzip压缩数据
- openresty使用gzip压缩解压
- 用GZip压缩和解压(.Net)
- C# GZip、Deflate 压缩解压算法函数
- C# GZip对字符串压缩和解压
- php的多态实现(转)
- PHP程序编写规则
- 每天一个 Linux 命令(31): /etc/group文件详解
- Linux系统下的svn常用命令
- Linux系统下的vi编辑命令
- GZip压缩解压
- mysql常用命令
- linux下的grep详细命令
- Shell变量赋值失败和引用不成功的原因(入门类)
- 改变SecureCrt的Session标题(转)
- 228. Summary Ranges
- Shell中的标志代表意义与条件判断(转)
- Linux下rpm常用命令
- Hadoop-- 海量文件的分布式计算处理方案(转)