Changeset 10250 for trunk/gli/src/org/greenstone/gatherer/util
- Timestamp:
- 2005-07-18T14:14:43+12:00 (19 years ago)
- Location:
- trunk/gli/src/org/greenstone/gatherer/util
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/util/Utility.java
r10214 r10250 42 42 import java.net.*; 43 43 import java.util.*; 44 import java.util.zip.*;45 44 // Don't even think about adding import javax.swing.* here! 46 45 // The functions in this class should not use any graphical classes. Put your function somewhere else buster! … … 443 442 444 443 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 exist474 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 disk481 482 // set up input stream483 InputStream zis = zipfile.getInputStream(zipentry);484 BufferedInputStream bzis = new BufferedInputStream(zis);485 DataInputStream dbzis = new DataInputStream(bzis);486 487 // set up output stream488 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 up525 * @param prefix_strip the <strong>int</strong> used to substring the file_path526 * @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 expression541 //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 expression547 //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 prefix582 String zip_path = file_path.substring(prefix_strip);583 // Zip files use '/' for directory separator584 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 writes597 // to the zip output stream until the bis has been entirely read.598 while ((byteCount = bis.read(data, 0, 1024)) > -1) {599 //Check for cancell600 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_path623 * @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 separator649 String unix_style_path650 = 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 function663 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 used681 * 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) and683 * {@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 separator728 String unix_style_path729 = 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 exist783 if (!zfile.exists() && !zfile.mkdirs()) {784 System.err.println("Error: unable to create directory " + zfile);785 }786 }787 else {788 // Write out file to disk789 790 //Make sure it's parent directory exists.791 File dir = new File(zfile.getParent());792 dir.mkdirs();793 794 // set up input stream795 InputStream zis = zipfile.getInputStream(zipentry);796 BufferedInputStream bzis = new BufferedInputStream(zis);797 DataInputStream dbzis = new DataInputStream(bzis);798 799 // set up output stream800 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 file821 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 843 444 static public String getSitesDir(String gsdl3_path) { 844 445 return gsdl3_path + File.separator + "web" -
trunk/gli/src/org/greenstone/gatherer/util/Zipup.java
r10214 r10250 42 42 /** 43 43 * 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)}. 45 45 * This class basically contains a main method that requires adequate arguments to call the zipup method. 46 46 * … … 52 52 * <br>[reject_expression], a regular expression of files to exclude from the archive. Optional. 53 53 * 54 * @see Utility.zipup(String, String, String, GShell, String, String)54 * @see ZipTools.zipup(String, String, String, GShell, String, String) 55 55 */ 56 56 public class Zipup { … … 91 91 } 92 92 93 Utility.zipup(base_dir, collection, directory, null, accept_expr, reject_expr);93 ZipTools.zipup(base_dir, collection, directory, null, accept_expr, reject_expr); 94 94 } 95 95 }
Note:
See TracChangeset
for help on using the changeset viewer.