home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 October / PCO1097.ISO / FilesBBS / WIN95 / IAVAZIP.EXE / DATA.Z / processImport.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-08-18  |  7.3 KB  |  349 lines

  1. package com.sfs.iavazip;
  2.  
  3. import com.sfs.awt.treeview.Folder;
  4. import com.sfs.awt.treeview.Item;
  5. import com.sfs.debug.Debug;
  6. import com.sfs.os.OS;
  7. import com.sfs.util.SortableVector;
  8. import com.sfs.util.ZipEntrySorter;
  9. import java.awt.Dialog;
  10. import java.awt.Window;
  11. import java.io.File;
  12. import java.io.FileInputStream;
  13. import java.io.FileNotFoundException;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import java.util.Enumeration;
  18. import java.util.Vector;
  19. import java.util.zip.ZipEntry;
  20. import java.util.zip.ZipOutputStream;
  21.  
  22. class processImport extends processDlg {
  23.    ImportDialog _import;
  24.    ZipOutputStream tempZip;
  25.    SortableVector importFiles;
  26.    SortableVector copyEntries;
  27.    Vector newZipEntries;
  28.    String destinationPath;
  29.    FileInputStream inFile;
  30.    // $FF: renamed from: is java.io.InputStream
  31.    InputStream field_0;
  32.  
  33.    void buildLists() {
  34.       ZipEntrySorter2 var1 = new ZipEntrySorter2(this);
  35.       this.importFiles = this._import.getSelection();
  36.       this.newZipEntries = new Vector();
  37.       Enumeration var2 = this.importFiles.elements();
  38.  
  39.       while(var2.hasMoreElements()) {
  40.          this.newZipEntries.addElement(this.createZipEntryName((Item)var2.nextElement()));
  41.       }
  42.  
  43.       this.copyEntries = new SortableVector(1, new ZipEntrySorter());
  44.       if (!super.mainApp.ZipIsEmpty) {
  45.          var2 = super.mainApp.zipFile.entries();
  46.  
  47.          while(var2.hasMoreElements()) {
  48.             this.copyEntries.add(var2.nextElement());
  49.          }
  50.       }
  51.  
  52.       switch (this._import.getAction()) {
  53.          case 0:
  54.          case 2:
  55.             for(int var10 = 0; var10 < this.newZipEntries.size(); ++var10) {
  56.                int var8;
  57.                if ((var8 = this.copyEntries.lookup(this.newZipEntries.elementAt(var10), var1)) != -1) {
  58.                   if (var10 % 20 == 0) {
  59.                      Thread.yield();
  60.                   }
  61.  
  62.                   this.copyEntries.removeElementAt(var8);
  63.                   Debug.Info("remove from copyEntries: " + this.newZipEntries.elementAt(var10));
  64.                }
  65.             }
  66.             break;
  67.          case 1:
  68.             for(int var9 = 0; var9 < this.importFiles.size(); ++var9) {
  69.                int var7;
  70.                if ((var7 = this.copyEntries.lookup(this.newZipEntries.elementAt(var9), var1)) != -1) {
  71.                   File var11 = new File(((Item)this.importFiles.elementAt(var9)).getAbsoluteName());
  72.                   if (var11.lastModified() > ((ZipEntry)this.copyEntries.elementAt(var7)).getTime()) {
  73.                      Debug.Info("remove from copyEntries: " + this.copyEntries.elementAt(var9));
  74.                      this.copyEntries.removeElementAt(var7);
  75.                      continue;
  76.                   }
  77.  
  78.                   if (var9 % 10 == 0) {
  79.                      Thread.yield();
  80.                   }
  81.                }
  82.  
  83.                Debug.Info("remove from importFiles: " + this.importFiles.elementAt(var9));
  84.                this.importFiles.removeElementAt(var9);
  85.                this.newZipEntries.removeElementAt(var9);
  86.                --var9;
  87.             }
  88.             break;
  89.          case 3:
  90.             for(int var4 = 0; var4 < this.importFiles.size(); ++var4) {
  91.                int var3;
  92.                if ((var3 = this.copyEntries.lookup(this.newZipEntries.elementAt(var4), var1)) != -1) {
  93.                   File var5 = new File(((Item)this.importFiles.elementAt(var4)).getAbsoluteName());
  94.                   if (var5.lastModified() > ((ZipEntry)this.copyEntries.elementAt(var3)).getTime()) {
  95.                      this.copyEntries.removeElementAt(var3);
  96.                      Debug.Info("remove from copyEntries: " + this.newZipEntries.elementAt(var4));
  97.                   } else {
  98.                      Debug.Info("remove from importFiles: " + this.importFiles.elementAt(var4));
  99.                      this.importFiles.removeElementAt(var4);
  100.                      this.newZipEntries.removeElementAt(var4);
  101.                      --var4;
  102.                   }
  103.  
  104.                   if (var4 % 10 == 0) {
  105.                      Thread.yield();
  106.                   }
  107.                }
  108.             }
  109.       }
  110.  
  111.       Debug.Info(this.copyEntries.toString());
  112.       Debug.Info(this.importFiles.toString());
  113.    }
  114.  
  115.    public void processCanceled() {
  116.       super.thread.stop();
  117.  
  118.       try {
  119.          if (this.field_0 != null) {
  120.             this.field_0.close();
  121.          }
  122.  
  123.          if (this.inFile != null) {
  124.             this.inFile.close();
  125.          }
  126.  
  127.          if (this.tempZip != null) {
  128.             this.tempZip.close();
  129.             File var1 = new File("temp.zip");
  130.             var1.delete();
  131.          }
  132.       } catch (IOException var2) {
  133.       }
  134.  
  135.       this._import.mainApp.setDefaultCursor();
  136.       super.mainApp.showStatus("Import Files canceled.");
  137.    }
  138.  
  139.    protected void zip(Item var1, String var2) {
  140.       try {
  141.          String var3 = var1.getAbsoluteName();
  142.          if (!OS.hasDrives()) {
  143.             var3 = var3.replace('\\', File.separatorChar);
  144.             var3 = var3.substring(1);
  145.          }
  146.  
  147.          this.inFile = new FileInputStream(var3);
  148.          var2 = var2.replace('\\', '/');
  149.          ZipEntry var4 = new ZipEntry(var2);
  150.          File var5 = new File(var1.getAbsoluteName());
  151.          var4.setTime(var5.lastModified());
  152.          int var6 = 0;
  153.          byte[] var7 = new byte[256];
  154.  
  155.          try {
  156.             this.tempZip.putNextEntry(var4);
  157.  
  158.             while((var6 = this.inFile.read(var7)) != -1) {
  159.                this.tempZip.write(var7, 0, var6);
  160.                Thread.yield();
  161.             }
  162.  
  163.             this.inFile.close();
  164.             this.tempZip.closeEntry();
  165.             if (this._import.getAction() == 2 && !var5.delete()) {
  166.                Debug.Info("delete on ACTION_MOVE failed.");
  167.                return;
  168.             }
  169.          } catch (IOException var10) {
  170.             this._import.mainApp.showStatus("IOException occured while reading " + var1.getAbsoluteName());
  171.             Debug.Info("IOException occured: " + ((Throwable)var10).getMessage());
  172.             ((Throwable)var10).printStackTrace(System.out);
  173.             return;
  174.          }
  175.       } catch (FileNotFoundException var11) {
  176.          this._import.mainApp.showStatus("File " + var1.getAbsoluteName() + " not found.");
  177.          ((Throwable)var11).printStackTrace(System.out);
  178.       }
  179.  
  180.    }
  181.  
  182.    public void run() {
  183.       Item var1 = this._import.mainApp.tw.Root.getCaretItem();
  184.       if (!(var1 instanceof Folder)) {
  185.          var1 = var1.getParent();
  186.       }
  187.  
  188.       if (var1 == this._import.mainApp.tw.Root) {
  189.          this.destinationPath = "";
  190.       } else {
  191.          String var2 = var1.getAbsoluteName();
  192.          this.destinationPath = var2.substring(var2.indexOf(92) + 1);
  193.          if (this.destinationPath.length() != 0) {
  194.             String var10001 = this.destinationPath;
  195.             this.destinationPath = var10001 + File.separator;
  196.          }
  197.       }
  198.  
  199.       ((processDlg)this).showStatus("preparing lists");
  200.       this.buildLists();
  201.       System.runFinalization();
  202.       System.gc();
  203.       if (this.newZipEntries.size() > 0) {
  204.          try {
  205.             this.tempZip = new ZipOutputStream(new FileOutputStream("temp.zip"));
  206.          } catch (IOException var14) {
  207.             ((Throwable)var14).printStackTrace(System.out);
  208.          }
  209.       }
  210.  
  211.       if (this.importFiles.size() == 0) {
  212.          super.mainApp.showStatus("Import 0 files completed.");
  213.          ((Window)this).dispose();
  214.       } else {
  215.          ((processDlg)this).setMaximum(this.importFiles.size() + this.copyEntries.size());
  216.          int var17 = 0;
  217.          if (!this._import.mainApp.ZipIsEmpty) {
  218.             ((processDlg)this).showHeading("Copy zip entries ...");
  219.             this.tempZip.setMethod(this._import.getMethod());
  220.             this.tempZip.setLevel(this._import.getCompression());
  221.             byte[] var5 = new byte[256];
  222.             Enumeration var6 = this.copyEntries.elements();
  223.  
  224.             try {
  225.                while(var6.hasMoreElements()) {
  226.                   ZipEntry var3 = (ZipEntry)var6.nextElement();
  227.                   ZipEntry var4 = new ZipEntry(var3.getName());
  228.                   this.tempZip.putNextEntry(var4);
  229.                   this.field_0 = this._import.mainApp.zipFile.getInputStream(var3);
  230.                   String var7 = var3.getName();
  231.                   Debug.Info("copying " + var7);
  232.                   int var8;
  233.                   if ((var8 = var7.lastIndexOf(var7)) != -1) {
  234.                      var7 = var7.substring(var8 + 1);
  235.                   }
  236.  
  237.                   ((processDlg)this).showStatus("copying " + var7);
  238.  
  239.                   int var9;
  240.                   while((var9 = this.field_0.read(var5)) != -1) {
  241.                      this.tempZip.write(var5, 0, var9);
  242.                      Thread.yield();
  243.                   }
  244.  
  245.                   this.tempZip.closeEntry();
  246.                   this.field_0.close();
  247.  
  248.                   try {
  249.                      Thread.sleep(15L);
  250.                   } catch (InterruptedException var13) {
  251.                   }
  252.                }
  253.             } catch (IOException var15) {
  254.                Debug.Info(((Throwable)var15).getMessage());
  255.                ((Throwable)var15).printStackTrace(System.out);
  256.             }
  257.          }
  258.  
  259.          ((processDlg)this).showHeading("Importing files");
  260.          Enumeration var18 = this.importFiles.elements();
  261.          Enumeration var19 = this.newZipEntries.elements();
  262.  
  263.          while(super.notCanceled && var18.hasMoreElements()) {
  264.             var19.hasMoreElements();
  265.             var1 = (Item)var18.nextElement();
  266.             Thread.yield();
  267.             ((processDlg)this).showStatus("import " + var1.getName());
  268.             this.zip(var1, (String)var19.nextElement());
  269.             super.pb.setProgress((float)(var17++));
  270.  
  271.             try {
  272.                Thread.sleep(15L);
  273.             } catch (InterruptedException var12) {
  274.             }
  275.          }
  276.  
  277.          try {
  278.             if (this.importFiles.size() > 0) {
  279.                this.tempZip.finish();
  280.                this.tempZip.close();
  281.                this.tempZip = null;
  282.             }
  283.          } catch (IOException var11) {
  284.             Debug.Info("sdfsfsdf" + ((Throwable)var11).getMessage());
  285.             ((Throwable)var11).printStackTrace(System.out);
  286.          }
  287.  
  288.          super.mainApp.cleanup();
  289.          super.mainApp.showStatus("Import " + Integer.toString(this.importFiles.size()) + " files completed.");
  290.          File var20 = new File("temp.zip");
  291.          File var21 = new File(super.mainApp.zipFileName);
  292.          if (!var21.delete()) {
  293.             super.mainApp.showStatus("Deleting old zip file failed.");
  294.          }
  295.  
  296.          if (!var20.renameTo(var21)) {
  297.             super.mainApp.showStatus("Can't rename temp.zip. Opening temp.zip.");
  298.             super.mainApp.Open("temp.zip");
  299.          } else {
  300.             super.mainApp.Open(super.mainApp.zipFileName);
  301.          }
  302.  
  303.          ((Window)this).dispose();
  304.       }
  305.    }
  306.  
  307.    public processImport(ImportDialog var1) {
  308.       super(var1.mainApp, "Import Files ...");
  309.       this._import = var1;
  310.       ((Dialog)this).setResizable(true);
  311.    }
  312.  
  313.    String createZipEntryName(Item var1) {
  314.       String var2 = var1.getAbsoluteName();
  315.       if (OS.hasDrives()) {
  316.          var2 = var2.substring(1);
  317.       }
  318.  
  319.       var2 = var2.replace('\\', '/');
  320.       switch (this._import.getFilenameHandling()) {
  321.          case 1:
  322.             int var3;
  323.             if (OS.hasDrives() && (var3 = var2.indexOf(":")) != -1) {
  324.                return this.destinationPath + var2.substring(var3 + 2);
  325.             }
  326.  
  327.             return this.destinationPath + var2;
  328.          case 2:
  329.             return this.destinationPath + var1.getName();
  330.          default:
  331.             int var8 = var1.getSelectedDistance();
  332.             if (var8 > 0) {
  333.                ++var8;
  334.  
  335.                Object var4;
  336.                for(var4 = var1; var8-- > 0; var4 = ((Item)var4).getParent()) {
  337.                }
  338.  
  339.                int var5 = ((Item)var4).getAbsoluteName().length();
  340.                var2 = var2.substring(var5);
  341.             } else {
  342.                var2 = var1.getName();
  343.             }
  344.  
  345.             return this.destinationPath + var2;
  346.       }
  347.    }
  348. }
  349.