gzip代碼

這個接着上一篇的,上一篇的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;
   }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章