博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
gzip代码
阅读量:3967 次
发布时间:2019-05-24

本文共 5342 字,大约阅读时间需要 17 分钟。

这个接着上一篇的,上一篇的gzip可能只有图片,没有详细的代码,这一次加上详细的代码,方便大佬们的指正,希望同学们也可以从中获取一点知识嘿嘿。(完整的带上swing窗口可选择压缩解压,并可以选择文件路径位置)

package javaapplication6;import java.awt.*;import java.awt.BorderLayout;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.*;import java.io.File;import java.util.*;import java.util.zip.GZIPInputStream;import java.util.zip.GZIPOutputStream;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class window extends JFrame implements ActionListener{
private static Object gzip; private static Object ungzip; private JButton display; private JPanel panel; private double result; private String lastCommand; public String writepath; public byte[] ret1; public String s; public String b; JButton q = new JButton("打开文件"); JFrame frame=new JFrame("Java文本框组件示例"); //创建Frame窗口 JPanel jp=new JPanel(); //创建面板 JTextField txtfield1=new JTextField(); //创建文本框public static void main(String[] args) {
// TODO 自动生成的方法存根 new window(); }
public window(){
setLayout(new BorderLayout()); ActionListener gzip = new zipAction(); ActionListener ungzip = new unzipAction(); panel = new JPanel(); panel.setLayout(new GridLayout(4, 4)); addButton("压缩", gzip); addButton("解压", ungzip); add(panel, BorderLayout.CENTER); q.setActionCommand("open"); q.setBackground(Color.GREEN);//设置按钮颜色 this.getContentPane().add(q, BorderLayout.SOUTH);//建立容器使用边界布局 q.addActionListener(this); txtfield1.setText("地址文本框"); //设置文本框的内 txtfield1.setHorizontalAlignment(JTextField.CENTER); //居中对齐 jp.add(txtfield1); this .add(jp,BorderLayout.WEST); this.add(panel); this.setBounds(600,600,600,600); this.setTitle("gzip压缩解压");this.setSize(333, 288);this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}
public void actionPerformed(ActionEvent e){
if (e.getActionCommand().equals("open")) {
JFileChooser jf = new JFileChooser(); jf.showOpenDialog(this);//显示打开的文件对话框 File f = jf.getSelectedFile();//使用文件类获取选择器选择的文件 String s = f.getAbsolutePath();//返回路径名 txtfield1.setText(s); } }private void addButton(String label, ActionListener listener) {
JButton button = new JButton(label); button.addActionListener(listener); panel.add(button); }
public class zipAction implements ActionListener   {
public void actionPerformed(ActionEvent event) {
s=txtfield1.getText(); File file = new File(s); System.out.println(s); try {
FileInputStream in = new FileInputStream(file); byte[] data = new byte[in.available()]; in.read(data); in.close(); System.out.println("文件原始大小:" + data.length); byte[] ret1 =gzips(data); Scanner jj=new Scanner(System.in); System.out.println("中转位置"); String writepath=s; System.out.println("文件压缩之后大小:" + ret1.length); FileOutputStream fos = new FileOutputStream(writepath); fos.write(ret1); fos.close(); JOptionPane.showMessageDialog(null, "压缩成功", "标题",JOptionPane.WARNING_MESSAGE); } catch(Exception e){
} }} public class unzipAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
try {
//测试压缩 s=txtfield1.getText(); File file1 = new File(s);//路径的读取之后转换为文件 FileInputStream with = new FileInputStream(file1);//建立文件输入流with byte[] giv = new byte[with.available()];//将文件转化为字节类型数组byte[] giv with.read(giv);//读取字节数组giv with.close();//关闭文件输入流 System.out.println("解压前的大小" + giv.length); String writePath=s; byte[] ret2 =ungzips(giv);//解压giv,之后转换为字节类型数组byte[] ret2 System.out.println("还原之后大小:" + ret2.length); FileOutputStream fos = new FileOutputStream(writePath);//建立文件输出流fos fos.write(ret2);//利用文件输出流写出ret2文件 fos.close();//关闭文件输出流 JOptionPane.showMessageDialog(null, "解压成功", "标题",JOptionPane.WARNING_MESSAGE); } catch(Exception e){
System.out.println("不是压缩后的文件"); } } } public byte[] ungzips(byte[] data) throws Exception{
ByteArrayInputStream bis = new ByteArrayInputStream(data); GZIPInputStream gzip = new GZIPInputStream(bis); byte[] buf = new byte[1024]; int num = -1; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while ((num = gzip.read(buf, 0, buf.length)) != -1){
bos.write(buf, 0, num); } gzip.close(); bis.close(); byte[] ret = bos.toByteArray(); bos.flush(); bos.close(); return ret; } public static byte[] gzips(byte[] data) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(bos); gzip.write(data); gzip.finish(); gzip.close(); byte[] ret = bos.toByteArray(); bos.close(); return ret; }}

转载地址:http://npcki.baihongyu.com/

你可能感兴趣的文章
打包时sun.misc.ServiceConfigurationError
查看>>
摘自 管理自己[Managing Oneself]
查看>>
程序员开发大型应用程序的技巧
查看>>
远程团队管理的10条戒律
查看>>
在服务器上排除问题的头五分钟
查看>>
Diagnosing DFC Configuration Problems
查看>>
jboss java.lang.NoClassDefFoundError: Could not initialize class com.documentum.fc.client.DfClient
查看>>
Java~并发流程控制的手段CountDownLatch、CyclicBarrier、Semaphore和Exchanger工具类的学习和使用
查看>>
芯片常见封装
查看>>
什么是oc门
查看>>
上拉电阻 下拉电阻的汇总
查看>>
NTC热敏电阻的基本特性
查看>>
数字地和模拟地处理的基本原则
查看>>
集电极开路,漏极开路,推挽,上拉电…
查看>>
长尾式差分放大电路2
查看>>
十种精密整流电路
查看>>
红外线遥控原理
查看>>
放大电路的主要性能指标?
查看>>
稳压、调压、监控、DC/DC电路大全
查看>>
放大电路的主要性能指标?
查看>>