Changeset 10250


Ignore:
Timestamp:
2005-07-18T14:14:43+12:00 (19 years ago)
Author:
mdewsnip
Message:

Moved all of the ZIP functions out of Utility and into a new ZipTools class.

Location:
trunk/gli/src/org/greenstone/gatherer
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/Gatherer.java

    r10249 r10250  
    5050import org.greenstone.gatherer.util.StaticStrings;
    5151import org.greenstone.gatherer.util.Utility;
     52import org.greenstone.gatherer.util.ZipTools;
    5253
    5354
     
    159160            //   for documented example collections so "base this on
    160161            //   existing collection" works
    161             Utility.unzipFromJar(Utility.COLLECT_ZIP, gli_user_directory.toString());
     162            ZipTools.unzipFromJar(Utility.COLLECT_ZIP, gli_user_directory.toString());
    162163        }
    163164        else {
     
    189190        if (!metadata_directory.exists()) {
    190191        // dig out metadata.zip from JAR file and unzip it
    191         Utility.unzipFromJar(Utility.METADATA_ZIP, gli_user_directory.toString());
     192        ZipTools.unzipFromJar(Utility.METADATA_ZIP, gli_user_directory.toString());
    192193        }
    193194
  • trunk/gli/src/org/greenstone/gatherer/cdm/CollectionDesignManager.java

    r10240 r10250  
    4040import org.greenstone.gatherer.RemoteGreenstoneServer;
    4141import org.greenstone.gatherer.util.Utility;
     42import org.greenstone.gatherer.util.ZipTools;
    4243import org.w3c.dom.*;
    4344import org.xml.sax.*;
     
    232233        //If the GLI is running as an applet, and Format Features or Cross-Collection Searching have changed,
    233234        //upload etc/collect.cfg to the server to immediately reflect these changes.
    234         Utility.zipup(Gatherer.getCollectDirectoryPath(), collection_name, Utility.CONFIG_FILE, null, "", "");
     235        ZipTools.zipup(Gatherer.getCollectDirectoryPath(), collection_name, Utility.CONFIG_FILE, null, "", "");
    235236        RemoteGreenstoneServer.upload_url_zip(collection_name, "etc", "", null);
    236237    }
  • trunk/gli/src/org/greenstone/gatherer/gui/CreatePane.java

    r10240 r10250  
    5353import org.greenstone.gatherer.collection.Collection;
    5454import org.greenstone.gatherer.util.Utility;
     55import org.greenstone.gatherer.util.ZipTools;
    5556import org.greenstone.gatherer.shell.GBasicProgressMonitor;
    5657import org.greenstone.gatherer.shell.GBuildProgressMonitor;
     
    689690        else if(rebuildTypeRequired == CollectionDesignManager.BUILDCOL) {
    690691            //First upload the collect.cfg file
    691             Utility.zipup(Gatherer.getCollectDirectoryPath(), collection_name, Utility.CONFIG_FILE, null, "", "");
     692            ZipTools.zipup(Gatherer.getCollectDirectoryPath(), collection_name, Utility.CONFIG_FILE, null, "", "");
    692693            RemoteGreenstoneServer.upload_url_zip(collection_name, "etc", "", null);
    693694
  • trunk/gli/src/org/greenstone/gatherer/shell/GShell.java

    r10240 r10250  
    5757import org.greenstone.gatherer.util.StaticStrings;
    5858import org.greenstone.gatherer.util.Utility;
     59import org.greenstone.gatherer.util.ZipTools;
     60
    5961
    6062/** The <strong>GShell</strong> is reponsible for running a separately threaded process in the command shell. This is necessary for executing the Perl Scripts and also for other system related funcitonality.
     
    240242        if(Gatherer.c_man.getCollection().getFilesChanged() || !Gatherer.g_man.create_pane.isIncremental()) {
    241243            // zip up import folder, but exclude metadata
    242             Utility.zipup(collect_directory_path, col_name, "import", this, "", ".*metadata\\.xml");
     244            ZipTools.zipup(collect_directory_path, col_name, "import", this, "", ".*metadata\\.xml");
    243245
    244246            if(hasSignalledStop()) { return; }
     
    251253        if(Gatherer.c_man.getCollection().getMetadataChanged() || !Gatherer.g_man.create_pane.isIncremental()) {
    252254            // zip up metadata from import folder
    253             Utility.zipup(collect_directory_path, col_name, "import", this, ".*metadata\\.xml", "");
     255            ZipTools.zipup(collect_directory_path, col_name, "import", this, ".*metadata\\.xml", "");
    254256
    255257            if(hasSignalledStop()) { return; }
     
    262264        if(hasSignalledStop()) { return; }
    263265        // upload etc folder to server (need collect.cfg and any hfiles)
    264         Utility.zipup(collect_directory_path, col_name, "etc", this, "", "");
     266        ZipTools.zipup(collect_directory_path, col_name, "etc", this, "", "");
    265267
    266268        if(hasSignalledStop()) { return; }
     
    272274            // upload images/ directory to server
    273275            if(hasSignalledStop()) { return; }
    274             Utility.zipup(collect_directory_path, col_name, "images", this, "", "");
     276            ZipTools.zipup(collect_directory_path, col_name, "images", this, "", "");
    275277
    276278            if(hasSignalledStop()) { return; }
     
    554556            RemoteGreenstoneServer.download_url_zip(col_name, ".", this, "", "");
    555557            if (!hasSignalledStop()) {
    556             Utility.unzip(Gatherer.getCollectDirectoryPath(), col_name);
     558            ZipTools.unzip(Gatherer.getCollectDirectoryPath(), col_name);
    557559            }
    558560        }
     
    585587            return;
    586588            }
    587             Utility.unzip(Gatherer.getCollectDirectoryPath(), col_name);
     589            ZipTools.unzip(Gatherer.getCollectDirectoryPath(), col_name);
    588590           
    589591            if (progress!=null) {
     
    611613            RemoteGreenstoneServer.download_url_zip(col_name, "building", this, ".*build\\.cfg", "");
    612614            if (!hasSignalledStop()) {
    613             Utility.unzip(Gatherer.getCollectDirectoryPath(), col_name);
     615            ZipTools.unzip(Gatherer.getCollectDirectoryPath(), col_name);
    614616            }
    615617           
     
    654656            RemoteGreenstoneServer.download_url_zip(user_tmp_dir,cd_dir,this,"","");
    655657
    656             Utility.unzip(Configuration.gsdl_path, user_tmp_dir.substring(1));
     658            ZipTools.unzip(Configuration.gsdl_path, user_tmp_dir.substring(1));
    657659
    658660            if (progress!=null) {
  • trunk/gli/src/org/greenstone/gatherer/util/Utility.java

    r10214 r10250  
    4242import java.net.*;
    4343import java.util.*;
    44 import java.util.zip.*;
    4544// Don't even think about adding import javax.swing.* here!
    4645// The functions in this class should not use any graphical classes. Put your function somewhere else buster!
     
    443442
    444443
    445     /**
    446      * Method which unzips a given metadata resoure.
    447      * @param jar_zip_fname The name of the jar file as a <strong>String</strong>.
    448      * @param dst_dir The destination directory for unzipping, also as a <strong>String</strong>.
    449      * @see JarTools.extractFromJar(String, String, boolean)
    450      */
    451     static public void unzipFromJar(String jar_zip_fname, String dst_dir) {
    452 
    453     File file = null;
    454     if (!dst_dir.endsWith(File.separator)) {
    455         dst_dir += File.separator;
    456     }
    457 
    458     JarTools.extractFromJar(jar_zip_fname,dst_dir,true);
    459 
    460     String zip_ofname = dst_dir + jar_zip_fname;
    461     File zip_file = new File(zip_ofname);
    462 
    463     try {
    464         ZipFile zipfile = new ZipFile(zip_file);
    465        
    466         Enumeration e = zipfile.entries();
    467         while (e.hasMoreElements()) {
    468         ZipEntry zipentry = (ZipEntry) e.nextElement();
    469         String zentryname = dst_dir + zipentry.getName();
    470         DebugStream.println("    Unzipping: " + zentryname);
    471 
    472         if (zipentry.isDirectory()) {
    473             // Create named directory, if it doesn't already exist
    474             File zip_entry_file = new File(zentryname);
    475             if (!zip_entry_file.exists() && !zip_entry_file.mkdirs()) {
    476             System.err.println("Error: unable to create directory " + zip_entry_file);
    477             }
    478         }
    479         else {
    480             // Write out file to disk
    481 
    482             // set up input stream
    483             InputStream zis = zipfile.getInputStream(zipentry);
    484             BufferedInputStream bzis = new BufferedInputStream(zis);
    485             DataInputStream dbzis = new DataInputStream(bzis);
    486            
    487             // set up output stream
    488             FileOutputStream fzos = new FileOutputStream(zentryname);
    489             BufferedOutputStream bfzos = new BufferedOutputStream(fzos);
    490            
    491             byte[] buf = new byte[1024];
    492             int len;
    493             while ((len = dbzis.read(buf)) >= 0) {
    494             bfzos.write(buf,0,len);
    495             }
    496            
    497             dbzis.close();
    498             bfzos.close();
    499         }
    500         }
    501     }
    502     catch (ZipException error) {
    503         System.err.println("Error: Unable to open '"+zip_file.getAbsolutePath()+"'");
    504         DebugStream.printStackTrace(error);
    505     }
    506 
    507     catch (Exception error) {
    508         error.printStackTrace();
    509     }
    510     }
    511    
    512 
    513     static protected String unixStylePath(String path)
    514     {
    515     String unix_path = path.replace('\\','/');
    516     return unix_path;
    517     }
    518 
    519     /**
    520      * Method to handle zipping up of a file (not a directory).
    521      * Called by {@link zipup(String, String, String, GShell, String, String)} and {@link dirFunc(ZipOutputStream, String, int, GShell, boolean, String, String)}
    522      *
    523      * @param zos the zip output stream, as a <strong>ZipOutputStream</strong>
    524      * @param file_path the <strong>String</strong> containing the path to the file/directory tozip up
    525      * @param prefix_strip the <strong>int</strong> used to substring the file_path
    526      * @param source the <strong>GShell</strong> object that is calling this method, so that we can check to see if the process has been cancelled.
    527      *               This is null if GShell is not the calling object.
    528      * @param accept_expr a <strong>String</strong> containing a regular expression of files to include in the archive. All other files will be excluded.
    529      *                    There is a special case of ".*doc.xml", where the doc.xml files will be parsed to only include their metadata.
    530      * @param reject_expr a <strong>String</strong> containing a regular expression of files not to include in the archive.
    531      * @return boolean - true if the zip was created successfully(?).
    532      * @see dirFunc(ZipOutputStream, String, int, GShell, boolean, String, String)
    533      * @see zipup(String, String, String, GShell, String, String)
    534      */
    535     static protected boolean zipFunc(ZipOutputStream zos, String file_path, int prefix_strip, GShell source, boolean encountered_file, String accept_expr, String reject_expr)
    536     {
    537     String new_file_path = file_path;
    538 
    539     if((reject_expr != "") && (file_path.matches(reject_expr))) {
    540         // matches reject expression
    541         //DebugStream.println("File \'" + file_path + "\' matches the reject expression \'" + reject_expr + "\'");
    542         return encountered_file;
    543     }
    544 
    545     if ((accept_expr != "") && (!file_path.matches(accept_expr))) {
    546         // does not match accept expression
    547         //DebugStream.println("File \'" + file_path + "\' doesn't match accept expression \'" + accept_expr + "\'");
    548         return encountered_file;
    549     }
    550 
    551     //Special case: we only want the metadata. Parse the xml files.
    552     if(accept_expr.compareTo(".*doc.xml") == 0) {
    553         DebugStream.println("Only want to get metadata");
    554         Document old_document = XMLTools.parseXMLFile(new File(file_path));
    555         NodeList content = old_document.getElementsByTagName("Content");
    556 
    557         try {
    558         for(int i=content.getLength()-1; i >= 0; i--) {
    559             //Remove all content - just leave the metadata.
    560             content.item(i).getParentNode().removeChild(content.item(i));
    561         }
    562         }
    563         catch (Exception ex) {
    564         ex.printStackTrace();
    565         }
    566 
    567         new_file_path = file_path.substring(0, file_path.lastIndexOf(File.separator) + 1) + "new.xml";
    568         //Is there any better way than writing out as a new file, then reading back in??
    569         XMLTools.writeXMLFile(new File(new_file_path), old_document);
    570     }
    571 
    572     // Using try is required because of file io.
    573     try {
    574         // Exit if pressed cancel.
    575         if (source != null && source.hasSignalledStop()) {
    576         return false;
    577         }
    578 
    579         // Create a Zip Entry and put it into the archive (no data yet).
    580 
    581         // Strip off col_dir prefix
    582         String zip_path = file_path.substring(prefix_strip);
    583         // Zip files use '/' for directory separator
    584         String unix_style_path = unixStylePath(zip_path);
    585 
    586         ZipEntry fileEntry = new ZipEntry(unix_style_path);
    587         zos.putNextEntry(fileEntry);
    588 
    589         // Create a file input stream and a buffered input stream.
    590         FileInputStream fis = new FileInputStream(new_file_path);
    591         BufferedInputStream bis = new BufferedInputStream(fis);
    592        
    593         // Create a byte array object named data and declare byte count variable.
    594         byte[] data = new byte[1024];
    595         int byteCount;
    596         // Create a loop that reads from the buffered input stream and writes
    597         // to the zip output stream until the bis has been entirely read.
    598         while ((byteCount = bis.read(data, 0, 1024)) > -1) {
    599         //Check for cancell
    600         if(source != null) {
    601             if(source.hasSignalledStop()) {
    602             break;
    603             }
    604         }
    605         zos.write(data, 0, byteCount);
    606         }
    607         encountered_file = true;
    608     }
    609     catch (IOException e) {
    610         e.printStackTrace();
    611     }
    612    
    613     DebugStream.println("Zipping up: " + file_path);
    614     return encountered_file;
    615     }
    616 
    617     /**
    618      * Method to handle ziping up of a directory. This is called by {@link zipup(String, String, String, GShell, String, String)}
    619      *
    620      * @param zos the <strong>ZipOutputStream</strong> to output to.
    621      * @param dir_name the directory name to zip, as a <strong>String</strong>
    622      * @param prefix_strip the <strong>int</strong> used to substring the file_path
    623      * @param source the <strong>GShell</strong> object that is calling this method, so that we can check to see if the process has been cancelled.
    624      *               This is null if GShell is not the calling object.
    625      * @param encountered_file ??What's this??
    626      * @param accept_expr a <strong>String</strong> containing a regular expression of files to include in the archive. All other files will be excluded.
    627      * @param reject_expr a <strong>String</strong> containing a regular expression of files not to include in the archive.
    628      *
    629      * @see zipup(String, String, String, GShell, String, String)
    630      * @see zipFunc(ZipOutputStream, String, int, GShell, boolean, String, String)
    631      */
    632     static protected boolean dirFunc (ZipOutputStream zos, String dir_name, int prefix_strip, GShell source, boolean encountered_file, String accept_expr, String reject_expr)
    633     {
    634     File dirObj = new File(dir_name);
    635 
    636     if (dirObj.exists() == true) {
    637         if (dirObj.isDirectory() == true) {
    638         // Create an array of File objects, one for each file or directory in dirObj.
    639         File [] fileList = dirObj.listFiles();
    640        
    641         // Loop through File array and display.
    642         for (int i = 0; i < fileList.length; i++) {
    643             if(source != null && source.hasSignalledStop()) { break; }
    644             File file = fileList[i];
    645             if (file.isDirectory()) {
    646             String dir_path = file.getPath();
    647             String zip_path = dir_path.substring(prefix_strip);
    648             // Zip files use '/' for directory separator
    649             String unix_style_path
    650                 = unixStylePath(zip_path+File.separator);
    651             ZipEntry dirEntry = new ZipEntry(unix_style_path);
    652 
    653             try {
    654                 zos.putNextEntry(dirEntry);
    655             }
    656             catch (IOException e) {
    657                 e.printStackTrace();
    658             }
    659 
    660             encountered_file = dirFunc(zos, dir_path, prefix_strip, source, encountered_file, accept_expr, reject_expr);
    661             } else if (file.isFile()) {
    662             // Call the zipFunc function
    663             String file_path = fileList[i].getPath();
    664             encountered_file = zipFunc(zos,file_path,prefix_strip, source, encountered_file, accept_expr, reject_expr);
    665             }
    666         }
    667         }
    668         else {
    669         System.err.println (dir_name+" is not a directory.");
    670         }
    671     }
    672     else {
    673         System.err.println ("Directory "+dir_name+" does not exist.");
    674     }
    675     return encountered_file;
    676     }
    677    
    678     /**
    679      * Method to zip up a given file or directory associated with a particular collection.
    680      * This method mainly called from {@link GShell}, with a few exceptions. The zip files produced are used
    681      * to pass files (new files to add, metadata, etc) between the GLI applet and the Greenstone server.
    682      * The actual zipping of files and directories is handled by {@link zipFunc(ZipOutputStream, String, int, GShell, boolean, String, String) and
    683      * {@link dirFunc(ZipOutputStream, String, int, GShell, boolean, String, String)} respectfully.
    684      *
    685      * @param col_dir the path to the collect directory, as a <strong>String</strong>
    686      * @param col_name the collection name, as as <strong>String</strong>
    687      * @param dir_or_file the directory or file within the collection to zip up, as a <strong>String</strong>
    688      * @param source the <strong>GShell</strong> object that is calling this method, so that we can check to see if the process has been cancelled.
    689      *               This is null if GShell is not the calling object.
    690      * @param accept_expr a <strong>String</strong> containing a regular expression of files to include in the archive. All other files will be excluded.
    691      * @param reject_expr a <strong>String</strong> containing a regular expression of files not to include in the archive.
    692      *
    693      * @see zipFunc(ZipOutputStream, String, int, GShell, boolean, String, String)
    694      * @see dirFunc(ZipOutputStream, String, int, GShell, boolean, String, String)
    695      */
    696     static public boolean zipup(String col_dir, String col_name, String dir_or_file, GShell source, String accept_expr, String reject_expr)
    697     {
    698     if (!col_dir.endsWith(File.separator)) {
    699         col_dir += File.separator;
    700     }
    701 
    702     int prefix_strip = col_dir.length();
    703     boolean encountered_file = false;
    704 
    705     String zip_fname = col_dir + col_name + ".zip";
    706     String zip_dir_or_file = col_dir + col_name + File.separator + dir_or_file;
    707     File zip_dof = new File(zip_dir_or_file);
    708 
    709     try {
    710         FileOutputStream fos = new FileOutputStream(zip_fname);
    711         ZipOutputStream zos = new ZipOutputStream(fos);
    712        
    713         if (zip_dof.exists()) {
    714         if (zip_dof.isDirectory()) {
    715             String zip_dir = zip_dir_or_file;
    716             encountered_file = dirFunc(zos, zip_dir, prefix_strip, source, encountered_file, accept_expr, reject_expr);
    717         }
    718         else {
    719             String zip_full_file = zip_dir_or_file;
    720             String zip_path = zip_full_file.substring(prefix_strip);
    721 
    722             for (int i=1; i<zip_path.length(); i++) {
    723             String ch = String.valueOf(zip_path.charAt(i));
    724 
    725             if (ch.equals(File.separator)) {
    726                 String dir_path = zip_path.substring(0,i);
    727                 // Zip files use '/' for directory separator
    728                 String unix_style_path
    729                 = unixStylePath(dir_path+File.separator);
    730                 ZipEntry dirEntry = new ZipEntry(unix_style_path);
    731                 zos.putNextEntry(dirEntry);
    732             }
    733             }
    734             encountered_file = zipFunc(zos, zip_full_file, prefix_strip, source, encountered_file, accept_expr, reject_expr);
    735         }
    736         }
    737         else {
    738         System.err.println("Warning: " + zip_dir_or_file + " does not exist!");
    739         }
    740        
    741         // Close the file output streams for both the file and the zip.
    742         zos.flush();
    743         zos.close();
    744         fos.close();
    745     }
    746     catch (IOException exception) {
    747         DebugStream.printStackTrace(exception);
    748         return false;
    749     }
    750     return encountered_file;
    751     }
    752    
    753     /**
    754      * Method to unzip a zip file associated with a particular collection.
    755      * The location and name of the zip file should be derrived from the collection. Please see below for details.
    756      *
    757      * @param col_dir the path to the collection directory, as a <strong>String</strong>. This is where the zip file should be located.
    758      * @param col_name the name of the collection, as a <strong>String</strong>. This should be the name of the zip file (with a  .zip extension).
    759      */
    760     static public void unzip(String col_dir, String col_name)
    761     {
    762     if (!col_dir.endsWith(File.separator)) {
    763         col_dir += File.separator;
    764     }
    765 
    766     String zip_fname = col_dir + col_name + ".zip";
    767     // int zip_mode = ZipFile.OPEN_READ | ZipFile.OPEN_DELETE;
    768     int zip_mode = ZipFile.OPEN_READ;
    769 
    770     try {
    771         File cfile = new File(zip_fname);
    772         ZipFile zipfile = new ZipFile(cfile, zip_mode);
    773        
    774         Enumeration e = zipfile.entries();
    775         while (e.hasMoreElements()) {
    776         ZipEntry zipentry = (ZipEntry) e.nextElement();
    777         String zentryname = col_dir + zipentry.getName();
    778         File zfile = new File(zentryname);
    779         DebugStream.println("    Unzipping: " + zentryname);
    780 
    781         if (zipentry.isDirectory()) {
    782             // Create named directory, if it doesn't already exist
    783             if (!zfile.exists() && !zfile.mkdirs()) {
    784             System.err.println("Error: unable to create directory " + zfile);
    785             }
    786         }
    787         else {
    788             // Write out file to disk
    789 
    790             //Make sure it's parent directory exists.
    791             File dir = new File(zfile.getParent());
    792             dir.mkdirs();
    793 
    794             // set up input stream
    795             InputStream zis = zipfile.getInputStream(zipentry);
    796             BufferedInputStream bzis = new BufferedInputStream(zis);
    797             DataInputStream dbzis = new DataInputStream(bzis);
    798            
    799             // set up output stream
    800             FileOutputStream fzos = new FileOutputStream(zentryname);
    801             BufferedOutputStream bfzos = new BufferedOutputStream(fzos);
    802            
    803             byte[] buf = new byte[1024];
    804             int len;
    805             while ((len = dbzis.read(buf)) >= 0) {
    806             bfzos.write(buf,0,len);
    807             }
    808            
    809             dbzis.close();
    810             bzis.close();
    811             zis.close();
    812 
    813             bfzos.close();
    814             fzos.close();
    815         }
    816         }
    817 
    818         zipfile.close();
    819 
    820         // delete zip file
    821         boolean file_deleted = cfile.delete();
    822         if (file_deleted) {
    823         System.err.println("Zip file " + cfile.toString() + " deleted");
    824         }
    825         else {
    826         System.err.println("Zip file " + cfile.toString() + " NOT deleted");
    827         }
    828 
    829 
    830     }
    831     catch (ZipException error) {
    832         System.err.println("Error: Unable to open '"+zip_fname+"'");
    833         System.err.println("This maybe caused by the zip file being empty.");
    834         DebugStream.printStackTrace(error);
    835     }
    836 
    837     catch (Exception error) {
    838         error.printStackTrace();
    839     }
    840     }
    841 
    842 
    843444    static public String getSitesDir(String gsdl3_path) {
    844445    return gsdl3_path + File.separator + "web"
  • trunk/gli/src/org/greenstone/gatherer/util/Zipup.java

    r10214 r10250  
    4242/**
    4343 * Zip up a given directory of a given Greenstone collection.
    44  * The actual zipping code is contained in {@link Utility.zipup(String, String, String, GShell, String, String)}.
     44 * The actual zipping code is contained in {@link ZipTools.zipup(String, String, String, GShell, String, String)}.
    4545 * This class basically contains a main method that requires adequate arguments to call the zipup method.
    4646 *
     
    5252 * <br>[reject_expression], a regular expression of files to exclude from the archive. Optional.
    5353 *
    54  * @see Utility.zipup(String, String, String, GShell, String, String)
     54 * @see ZipTools.zipup(String, String, String, GShell, String, String)
    5555 */
    5656public class Zipup {
     
    9191    }     
    9292
    93     Utility.zipup(base_dir, collection, directory, null, accept_expr, reject_expr);
     93    ZipTools.zipup(base_dir, collection, directory, null, accept_expr, reject_expr);
    9494    }
    9595}
Note: See TracChangeset for help on using the changeset viewer.