import java.io.*; import java.net.*; import java.util.zip.*; import java.awt.*; import java.awt.event.*; // Author: John Pritchard-Williams // Email : monojohnny@yahoo.com // Version : 1.0 // Developed in Windows 98 environment, using JDK1.1.6 from sun // Tested on Psion 5mx only - would appreciate confirmation on other platforms. // // Zippy was kept deliberately kept small (currently under 10k) - therefore minimal error // handling is performed. // FOR INSTANCE - NO "Overwrite y/n" dialogue is provided - zippy will simply overwrite // files when extracting - it is advisable therefore to make use of the "LIST CONTENTS" feature !! // // Current known problems / 'features': // // zippy can NOT handle absolute filenames (eg c:\temp\xyz.txt CANNOT be extracted). // zippy currently SKIPS any "META-INF/MANIFEST.MF" files. // zippy will ALWAYS try and use the directory "C:\DOCUMENTS\UNZIP" path // - assumes "C:\DOCUMENTS" exists, and creates ~UNZIP if not present // zippy will abort part-way-through an extraction process if a single file/directory extraction // fails. // zippy does not have an EPOC 'look and feel' // // Possible future enhancements: // // Provide a "select output directory" dialogue // Provide handling for absolute filenames // Bring look and feel in line with standard EPOC look and feel. // // NOTES ABOUT THIS SOURCE CODE // This source code was realised 'cos I thought it might be of some use to other EPOC users // - feel free to improve / modify / do whatever to it - FOR YOUR OWN USE ONLY. // PLEASE DO NOT MAKE PUBLIC ANY MODIFIED VERSIONS OF THIS CODE - in its source format or compiled format !!! // Thanks !!! John public class zippy extends Frame { String filename,out_dir,in_dir,sep,drive_letter; MenuItem o,l,x,e; TextArea t; String Version; public static void main(String[] args) { new zippy(); } zippy() { super("Zippy - No File Loaded"); // Set the version number here - gets echoed to the status area Version="1.0"; // Get the OS's path Separator character sep=File.separator; // Set the drive letter up drive_letter="C:"; // Now build up the output path (for relative filenames) out_dir=drive_letter+sep+"Documents"+sep+"Unzip"; // Make sure we've got a sensible layout this.setLayout(new BorderLayout(10,10)); // Make sure we have a way of closing the window !! // Doesn't work in EPOC - no close widget - need a menu item / hotkey this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { close(); } }); // Setup the Main Menu // Grey out List and Extract if filename is null MenuBar menubar=new MenuBar(); this.setMenuBar(menubar); Menu file = new Menu("File"); menubar.add(file); file.add(o=new MenuItem("Open Zip Archive",new MenuShortcut(KeyEvent.VK_O))); file.add(l=new MenuItem("List Contents of Zip Archive",new MenuShortcut(KeyEvent.VK_L))); file.add(x=new MenuItem("Extract Contents of Zip Archive",new MenuShortcut(KeyEvent.VK_X))); file.add(e=new MenuItem("Exit",new MenuShortcut(KeyEvent.VK_E))); if (filename == null) { x.setEnabled(false);} if (filename == null) { l.setEnabled(false);} // End of menu items // Now add the appropriate events on menu o.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { open_zip(); }}); l.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { list_zip(); }}); x.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { extract_zip(); }}); e.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { close(); }}); // Create and add the TextArea t = new TextArea("Welcome to Zippy...\n",15,50); t.setEditable(false); this.add(t,"Center"); this.pack(); this.show(); // Now append the welcome text t.append("Zippy is freeware. Use at your own risk !!!\n"); t.append("Version :"+Version+"\n"); t.append("Zippy will extract to :"+out_dir+"\n"); t.append("Comments or questions to monojohnny@yahoo.com\n"); } void open_zip() { int NumFiles,NumDirs; FileDialog f = new FileDialog(this,"Open Archive",FileDialog.LOAD); f.show(); filename=f.getFile(); in_dir=f.getDirectory(); if (filename != null) { x.setEnabled(true); l.setEnabled(true); this.setTitle("Zippy - "+filename); } t.append("Opening "+filename+"...\n"); try { FileInputStream fis= new FileInputStream(in_dir+"\\"+filename); ZipInputStream zpis=new ZipInputStream(fis); ZipEntry zie; String zfilename; long filesize; boolean isDir; NumFiles=0; NumDirs=0; while ( ( zie=zpis.getNextEntry() ) != null) { zfilename=zie.getName(); isDir=zie.isDirectory(); if (isDir) { NumDirs++; } else { NumFiles++;} // t.append(zfilename+"\n"); } fis.close(); t.append("Found "+NumFiles+" files and "+NumDirs+" Directories.\n"); t.append("Done.\n"); } catch (Exception e) { t.append("Error Opening "+e.getMessage()+".\n"); } ; } void list_zip() { t.append("Listing "+filename+"...\n"); try { FileInputStream fis= new FileInputStream(in_dir+"\\"+filename); ZipInputStream zpis=new ZipInputStream(fis); ZipEntry zie; String zfilename; long filesize; while ( ( zie=zpis.getNextEntry() ) != null) { zfilename=zie.getName(); t.append(zfilename+"\n"); } fis.close(); } catch (Exception e) { t.append("Error Listing "+e.getMessage()+".\n"); } ; } void extract_zip() { // out_dir=tf.getText(); t.append("Using Output Directory of "+out_dir+".\n"); // Just make the directory - even it is already exists File Dir = new File(out_dir); Dir.mkdir(); t.append("Extracting "+filename+"\n"); try { FileInputStream fis= new FileInputStream(in_dir+"\\"+filename); ZipInputStream zpis=new ZipInputStream(fis); ZipEntry zie; String zfilename; long filesize; boolean isDir; while ( ( zie=zpis.getNextEntry() ) != null) { zfilename=zie.getName(); isDir=zie.isDirectory(); if (isDir) { File f = new File(out_dir+"\\"+zfilename); f.mkdir(); } else if (!(zfilename.equals("META-INF/MANIFEST.MF"))) { byte[] buffer=new byte[4096]; int bytes_read; try { FileOutputStream fos= new FileOutputStream(out_dir+"\\"+zfilename); while ( ( bytes_read=zpis.read(buffer) ) != -1) { fos.write(buffer,0,bytes_read); } fos.close(); } catch(Exception e) { t.append("Error Extracting to "+e.getMessage()+" - unzip aborted\n"); } ; } } fis.close(); t.append("Done.\n"); } catch (Exception e) { t.append("Error Extracting "+e.getMessage()+" - unzip aborted\n"); } ; } void close() { System.exit(0); } ; }