Jumat, 01 Agustus 2025

JAVA - PROGRAM KOMPRESI FILE MENJADI GZip


 




/*

 * JFrameGZip.java

 *

 * Created on 18 Agustus 2008, 22:42

 */

 

 

 

/**

 *

 * @author  Administrator

 */

import java.util.zip.*;

import java.io.*;

import java.awt.event.*;

import javax.swing.*;

public class JFrameGZip extends javax.swing.JFrame implements ActionListener{

        int hasil,karakter;

                File nama_file;

                String nama="",output,input,extensi,zipper;

    /** Creates new form JFrameGZip */

    public JFrameGZip() {

        super("GZip File");

        initComponents();

        setResizable(false);

        tombolopen.addActionListener(this);

        tombolzip.addActionListener(this);

    }

    public void actionPerformed(ActionEvent x){

        if(x.getSource()==tombolopen){

            setTitle("Open File");

            open_file.setFileSelectionMode(JFileChooser.FILES_ONLY);

            open_file.setDialogTitle("Pilih File untuk Percobaan");

            hasil = open_file.showOpenDialog(null);

            if(hasil == JFileChooser.CANCEL_OPTION){

                setTitle("GZip File");

                return;

            }

            nama_file = open_file.getSelectedFile();

            nama=nama_file.getAbsolutePath();

            setTitle(nama);

            karakter=nama.length();

        }

        if(x.getSource()==tombolzip){

            if(!nama.equals("")){

                zipper="."+nama.charAt(karakter-4)+nama.charAt(karakter-3)+nama.charAt(karakter-2)+nama.charAt(karakter-1);

                if(!zipper.equals(".gzip")){

                    setTitle("GZipping File...");

                    extensi="."+nama.charAt(karakter-3)+nama.charAt(karakter-2)+nama.charAt(karakter-1);

                    open_file.setFileSelectionMode(JFileChooser.FILES_ONLY);

                    open_file.setDialogTitle("Simpan File GZip");

                    hasil = open_file.showSaveDialog(null);

                    if(hasil == JFileChooser.CANCEL_OPTION){

                        setTitle("GZip File");

                        return;

                    }

                    nama_file = open_file.getSelectedFile();

                    try {

                        output = nama_file.getAbsolutePath()+extensi+".gzip";

                        GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(output));

                        FileInputStream in = new FileInputStream(nama);

                        byte[] bufer = new byte[1024];

                        int panjang;

                        while ((panjang = in.read(bufer)) > 0){

                            out.write(bufer, 0, panjang);

                        }

                        in.close();

                        out.finish();

                        out.close();

                    } catch (IOException e) {}

                        setTitle(output);

                }

            else

                JOptionPane.showMessageDialog(this,"Yang ini sudah di-GZip, om.","Error",JOptionPane.ERROR_MESSAGE);

            }

        else

            JOptionPane.showMessageDialog(null,"Pilih Filenya dulu, om","Error",JOptionPane.WARNING_MESSAGE);

        }

    }

    /** This method is called from within the constructor to

     * initialize the form.

     * WARNING: Do NOT modify this code. The content of this method is

     * always regenerated by the Form Editor.

     */

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents

    private void initComponents() {

 

        open_file = new javax.swing.JFileChooser();

        panel1 = new javax.swing.JPanel();

        tombolopen = new javax.swing.JButton();

        tombolzip = new javax.swing.JButton();

        tombolexit = new javax.swing.JButton();

 

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

 

        tombolopen.setText("Open");

        tombolopen.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                tombolopenActionPerformed(evt);

            }

        });

 

        tombolzip.setText("Compress");

 

        tombolexit.setText("Exit");

        tombolexit.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {

                tombolexitActionPerformed(evt);

            }

        });

 

        javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);

        panel1.setLayout(panel1Layout);

        panel1Layout.setHorizontalGroup(

            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGroup(panel1Layout.createSequentialGroup()

                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

                    .addComponent(tombolopen, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

                    .addComponent(tombolzip, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

                    .addComponent(tombolexit, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

                .addContainerGap())

        );

        panel1Layout.setVerticalGroup(

            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGroup(panel1Layout.createSequentialGroup()

                .addContainerGap()

                .addComponent(tombolopen)

                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                .addComponent(tombolzip)

                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

                .addComponent(tombolexit)

                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

        );

 

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

        getContentPane().setLayout(layout);

        layout.setHorizontalGroup(

            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

                .addContainerGap(199, Short.MAX_VALUE)

                .addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

        );

        layout.setVerticalGroup(

            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addComponent(panel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

        );

 

        pack();

    }// </editor-fold>//GEN-END:initComponents

 

    private void tombolexitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tombolexitActionPerformed

        // TODO add your handling code here:

        System.exit(0);

    }//GEN-LAST:event_tombolexitActionPerformed

 

    private void tombolopenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tombolopenActionPerformed

        // TODO add your handling code here:

      

    }//GEN-LAST:event_tombolopenActionPerformed

   

    /**

     * @param args the command line arguments

     */

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                new JFrameGZip().setVisible(true);

            }

        });

    }

   

    // Variables declaration - do not modify//GEN-BEGIN:variables

    private javax.swing.JFileChooser open_file;

    private javax.swing.JPanel panel1;

    private javax.swing.JButton tombolexit;

    private javax.swing.JButton tombolopen;

    private javax.swing.JButton tombolzip;

    // End of variables declaration//GEN-END:variables

   

}

Tidak ada komentar: