/* * ZipAccess.java * * Created on March 27, 2002, 10:44 AM */ package subjectData; /** * * @author Joset A. Etzel */ import java.io.*; import java.util.*; import java.util.zip.*; //I added this - needed for file i/o & zip access import javax.swing.filechooser.*; import java.io.File; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ZipAccess extends javax.swing.JInternalFrame { static final int intBUFFER = 2048; //constant used for zip file access /** Creates new form ZipAccess */ public ZipAccess() { initComponents(); } /** 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. */ private void initComponents() {//GEN-BEGIN:initComponents cmdUnzip = new javax.swing.JButton(); txtInPath = new javax.swing.JTextField(); lblInFile = new javax.swing.JLabel(); cmdPickFile = new javax.swing.JButton(); cmdCompress = new javax.swing.JButton(); lblOutFile = new javax.swing.JLabel(); txtOutPath = new javax.swing.JTextField(); cmdPickZip = new javax.swing.JButton(); jPanel1 = new javax.swing.JPanel(); getContentPane().setLayout(new java.awt.GridBagLayout()); java.awt.GridBagConstraints gridBagConstraints1; setMaximizable(true); setTitle("Working with Zip Files!"); setIconifiable(true); setResizable(true); setClosable(true); cmdUnzip.setText("Unzip the File"); cmdUnzip.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdUnzipActionPerformed(evt); } }); gridBagConstraints1 = new java.awt.GridBagConstraints(); gridBagConstraints1.gridx = 2; gridBagConstraints1.gridy = 3; getContentPane().add(cmdUnzip, gridBagConstraints1); txtInPath.setPreferredSize(new java.awt.Dimension(200, 21)); txtInPath.setMinimumSize(new java.awt.Dimension(200, 21)); gridBagConstraints1 = new java.awt.GridBagConstraints(); gridBagConstraints1.gridx = 1; gridBagConstraints1.gridy = 0; gridBagConstraints1.ipady = 2; getContentPane().add(txtInPath, gridBagConstraints1); lblInFile.setText("File to Compress: "); gridBagConstraints1 = new java.awt.GridBagConstraints(); gridBagConstraints1.gridx = 0; gridBagConstraints1.gridy = 0; gridBagConstraints1.anchor = java.awt.GridBagConstraints.EAST; getContentPane().add(lblInFile, gridBagConstraints1); cmdPickFile.setText("Select File"); cmdPickFile.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdPickFileActionPerformed(evt); } }); gridBagConstraints1 = new java.awt.GridBagConstraints(); gridBagConstraints1.gridx = 2; gridBagConstraints1.gridy = 0; gridBagConstraints1.ipadx = 2; getContentPane().add(cmdPickFile, gridBagConstraints1); cmdCompress.setText("Compress the File"); cmdCompress.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdCompressActionPerformed(evt); } }); gridBagConstraints1 = new java.awt.GridBagConstraints(); gridBagConstraints1.gridx = 1; gridBagConstraints1.gridy = 3; getContentPane().add(cmdCompress, gridBagConstraints1); lblOutFile.setText("Zip File: "); gridBagConstraints1 = new java.awt.GridBagConstraints(); gridBagConstraints1.gridx = 0; gridBagConstraints1.gridy = 1; gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST; getContentPane().add(lblOutFile, gridBagConstraints1); txtOutPath.setPreferredSize(new java.awt.Dimension(200, 21)); txtOutPath.setMinimumSize(new java.awt.Dimension(200, 21)); gridBagConstraints1 = new java.awt.GridBagConstraints(); gridBagConstraints1.gridx = 1; gridBagConstraints1.gridy = 1; getContentPane().add(txtOutPath, gridBagConstraints1); cmdPickZip.setText("Select File"); cmdPickZip.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdPickZipActionPerformed(evt); } }); gridBagConstraints1 = new java.awt.GridBagConstraints(); gridBagConstraints1.gridx = 2; gridBagConstraints1.gridy = 1; getContentPane().add(cmdPickZip, gridBagConstraints1); jPanel1.setMinimumSize(new java.awt.Dimension(30, 30)); gridBagConstraints1 = new java.awt.GridBagConstraints(); gridBagConstraints1.gridx = 2; gridBagConstraints1.gridy = 2; getContentPane().add(jPanel1, gridBagConstraints1); pack(); }//GEN-END:initComponents private void cmdPickZipActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdPickZipActionPerformed //show a file chooser to let user pick a zip file for opening, or file to write if compressing. //JFileChooser comes with the ability to move around the directory structure JFileChooser frmFileOpen = new JFileChooser(); //declare a JFileChooser - the select file box frmFileOpen.setApproveButtonText("Select"); //have button say Select instead of Open int intResult = frmFileOpen.showOpenDialog(null); //this returns if select or cancel was clicked File fileChosen = frmFileOpen.getSelectedFile(); //the file picked, if any if (intResult == JFileChooser.APPROVE_OPTION) { txtOutPath.setText(fileChosen.getPath()); } else if (intResult == JFileChooser.CANCEL_OPTION) { txtOutPath.setText(""); } }//GEN-LAST:event_cmdPickZipActionPerformed private void cmdCompressActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCompressActionPerformed //compresses the file in txtPath; saves it as the same file name .zip try { BufferedInputStream origin = null; //FileOutputStream dest = new FileOutputStream("c:\\temp\\jotest.zip"); FileOutputStream dest = new FileOutputStream(txtOutPath.getText()); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); byte data[] = new byte[intBUFFER]; FileInputStream fi = new FileInputStream(txtInPath.getText()); origin = new BufferedInputStream(fi, intBUFFER); ZipEntry entry = new ZipEntry(txtInPath.getText()); out.putNextEntry(entry); int count; while((count = origin.read(data, 0, intBUFFER)) != -1) { out.write(data, 0, count); } origin.close(); out.close(); } catch(Exception e) { e.printStackTrace(); } }//GEN-LAST:event_cmdCompressActionPerformed private void cmdPickFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdPickFileActionPerformed //show a file chooser to let user pick a text file for compression. //JFileChooser comes with the ability to move around the directory structure JFileChooser frmFileOpen = new JFileChooser(); //declare a JFileChooser - the select file box frmFileOpen.setApproveButtonText("Select"); //have button say Select instead of Open int intResult = frmFileOpen.showOpenDialog(null); //this returns if select or cancel was clicked File fileChosen = frmFileOpen.getSelectedFile(); //the file picked, if any if (intResult == JFileChooser.APPROVE_OPTION) { txtInPath.setText(fileChosen.getPath()); } else if (intResult == JFileChooser.CANCEL_OPTION) { txtInPath.setText(""); } }//GEN-LAST:event_cmdPickFileActionPerformed private void cmdUnzipActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdUnzipActionPerformed int intInput = 0; try { BufferedInputStream is = null; ZipEntry entry; ZipFile zipfile = new ZipFile(txtOutPath.getText()); Enumeration e = zipfile.entries(); //used if more than one file in the zip file while (e.hasMoreElements()) { entry = (ZipEntry) e.nextElement(); System.out.println("Extracting: " + entry); is = new BufferedInputStream(zipfile.getInputStream(entry)); while ((intInput = is.read()) != -1) { System.out.print((char)intInput); } is.close(); } } catch(Exception e) { e.printStackTrace(); } }//GEN-LAST:event_cmdUnzipActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton cmdUnzip; private javax.swing.JTextField txtInPath; private javax.swing.JLabel lblInFile; private javax.swing.JButton cmdPickFile; private javax.swing.JButton cmdCompress; private javax.swing.JLabel lblOutFile; private javax.swing.JTextField txtOutPath; private javax.swing.JButton cmdPickZip; private javax.swing.JPanel jPanel1; // End of variables declaration//GEN-END:variables }