1. Dmitry Danileyko
  2. nbNetCatUpdater

Commits

Dmitry Danileyko  committed 7341d9d

Add auto unzip functionality.

  • Participants
  • Parent commits b8650f8
  • Branches default

Comments (0)

Files changed (3)

File src/org/narsereg/nbnetcatupdater/NBUpdaterDownloadFile.java

View file
  • Ignore whitespace
                 int numRead = 0;
                 int current = 0;
                 while (!isCancelled() &&(numRead = in.read(buffer)) != -1){
-                    
-//                    System.out.println(interruptThread);
-//                    if(interruptThread){
-//                        Thread.sleep(100);
-//                        Thread.currentThread().interrupt();
-//                        Thread.sleep(100);
-//                    }
-//                    Thread.sleep(100);
-//
-                    if(Thread.currentThread().isInterrupted()){
-                        try {
-                            Thread.currentThread().sleep(1000);
-                        } catch (InterruptedException ex) {
-                            break;
-                        }
-                        Thread.currentThread().isInterrupted();
-
-                        break;
-                    }
                     current += numRead;
                     publish(current);
                     out.write(buffer, 0, numRead);
-                    try {
-                        //Thread.currentThread().interrupt();                     
-
-                        
-                            Thread.sleep(1L);
-                    } catch (InterruptedException ex) {
-                        break;
-                    }
-                    
-//                        break;
-//                    }
-                    
-//                    if(interruptThread){
-//                        Thread.currentThread().interrupt();
-//                        try {
-//                            Thread.currentThread().sleep(1000);
-//                        } catch (InterruptedException ex) {
-//break;                        }
-//                    }
                 }
             }  finally {
                 out.flush();
         } catch (IOException ex) {
             Logger.getLogger(NBUpdaterDownloadFile.class.getName()).log(Level.SEVERE, null, ex);
         }
-        nbuf.pathToDownloadedFile = downloadedFile.getAbsolutePath();
+        nbuf.unzipDownloadedFile(downloadedFile.getAbsolutePath());
         return true;
     }
     
         return conn;
     }
     
-    private void closeConnection(){
-        conn.disconnect();
-    }
-    
     public int getFileSize(){
         return conn.getContentLength();
     }
     
-      @Override
-  protected void done() {
-    if (isCancelled())
-    {
-      closeConnection();
-      System.out.print("YES!!!");
-    }
-  }
-    
 }

File src/org/narsereg/nbnetcatupdater/NBUpdaterForm.form

View file
  • Ignore whitespace
                       <EmptySpace max="32767" attributes="0"/>
                       <Component id="chooseExtractToButton" min="-2" max="-2" attributes="0"/>
                   </Group>
-                  <Component id="runButton" alignment="0" max="32767" attributes="0"/>
-                  <Component id="extractButton" alignment="0" max="32767" attributes="0"/>
               </Group>
               <EmptySpace max="-2" attributes="0"/>
           </Group>
               </Group>
               <EmptySpace type="unrelated" max="-2" attributes="0"/>
               <Component id="extractToTextField" min="-2" max="-2" attributes="0"/>
-              <EmptySpace type="unrelated" max="-2" attributes="0"/>
-              <Component id="extractButton" min="-2" max="-2" attributes="0"/>
-              <EmptySpace type="unrelated" max="-2" attributes="0"/>
-              <Component id="runButton" min="-2" max="-2" attributes="0"/>
-              <EmptySpace pref="24" max="32767" attributes="0"/>
+              <EmptySpace max="32767" attributes="0"/>
           </Group>
       </Group>
     </DimensionLayout>
           </StringArray>
         </Property>
       </Properties>
+      <Events>
+        <EventHandler event="itemStateChanged" listener="java.awt.event.ItemListener" parameters="java.awt.event.ItemEvent" handler="platformComboBoxItemStateChanged"/>
+      </Events>
     </Component>
     <Component class="javax.swing.JProgressBar" name="downloadProgressBar">
     </Component>
     <Component class="javax.swing.JButton" name="chooseExtractToButton">
       <Properties>
         <Property name="text" type="java.lang.String" value="..."/>
+        <Property name="enabled" type="boolean" value="false"/>
       </Properties>
       <Events>
         <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="chooseExtractToButtonActionPerformed"/>
         <Property name="editable" type="boolean" value="false"/>
       </Properties>
     </Component>
-    <Component class="javax.swing.JToggleButton" name="runButton">
-      <Properties>
-        <Property name="text" type="java.lang.String" value="Run downloaded file"/>
-      </Properties>
-    </Component>
-    <Component class="javax.swing.JToggleButton" name="extractButton">
-      <Properties>
-        <Property name="text" type="java.lang.String" value="Extract"/>
-      </Properties>
-      <Events>
-        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="extractButtonActionPerformed"/>
-      </Events>
-    </Component>
   </SubComponents>
 </Form>

File src/org/narsereg/nbnetcatupdater/NBUpdaterForm.java

View file
  • Ignore whitespace
  */
 package org.narsereg.nbnetcatupdater;
 
+import java.io.*;
+import java.util.Enumeration;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipException;
+import java.util.zip.ZipFile;
 import javax.swing.JFileChooser;
 
 /**
         extractToLabel = new javax.swing.JLabel();
         chooseExtractToButton = new javax.swing.JButton();
         extractToTextField = new javax.swing.JTextField();
-        runButton = new javax.swing.JToggleButton();
-        extractButton = new javax.swing.JToggleButton();
 
         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
         platformLabel.setText("Platform");
 
         platformComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Windows", "Linux", "Solaris", "Solaris (sparc)", "OS X", "Zip" }));
+        platformComboBox.addItemListener(new java.awt.event.ItemListener() {
+            public void itemStateChanged(java.awt.event.ItemEvent evt) {
+                platformComboBoxItemStateChanged(evt);
+            }
+        });
 
         downloadButton.setText("Download");
         downloadButton.addActionListener(new java.awt.event.ActionListener() {
         extractToLabel.setText("Extract to");
 
         chooseExtractToButton.setText("...");
+        chooseExtractToButton.setEnabled(false);
         chooseExtractToButton.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
                 chooseExtractToButtonActionPerformed(evt);
 
         extractToTextField.setEditable(false);
 
-        runButton.setText("Run downloaded file");
-
-        extractButton.setText("Extract");
-        extractButton.addActionListener(new java.awt.event.ActionListener() {
-            public void actionPerformed(java.awt.event.ActionEvent evt) {
-                extractButtonActionPerformed(evt);
-            }
-        });
-
         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
         getContentPane().setLayout(layout);
         layout.setHorizontalGroup(
                     .addGroup(layout.createSequentialGroup()
                         .addComponent(extractToLabel)
                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
-                        .addComponent(chooseExtractToButton))
-                    .addComponent(runButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
-                    .addComponent(extractButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
+                        .addComponent(chooseExtractToButton)))
                 .addContainerGap())
         );
         layout.setVerticalGroup(
                     .addComponent(chooseExtractToButton))
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                 .addComponent(extractToTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
-                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
-                .addComponent(extractButton)
-                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
-                .addComponent(runButton)
-                .addContainerGap(24, Short.MAX_VALUE))
+                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
         );
 
         pack();
     }//GEN-LAST:event_chooseDownloadDirButtonActionPerformed
 
     private void downloadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downloadButtonActionPerformed
+        downloadButton.setEnabled(false);
         NBUpdaterUtils nbUtils = new NBUpdaterUtils();
         nbUtils.setOption(options[bundleComboBox.getSelectedIndex()]);
         nbUtils.setPlatform(platforms[platformComboBox.getSelectedIndex()]);
         NBUpdaterDownloadFile df = new NBUpdaterDownloadFile(nbUtils.getDownloadFileURL(), this);
-        
-        if (downloadButton.getText().equals("Download")) {
-            this.setComponentsEnabled(false);
-            
             downloadProgressBar.setMinimum(0);
             downloadProgressBar.setMaximum(df.getFileSize());
             downloadProgressBar.setStringPainted(true);
             df.execute();
-            downloadButton.setText("Cancel");
-        } else {
-            this.setComponentsEnabled(true);
-//            try {
-//                //df.
-//                df.cancel(true);
-//            } catch (java.lang.NullPointerException ex) {
-//                
-//            }
-//            df = null;
-            //df.cancel(true);
-            df.interruptThread = true;
-            df.cancel(true);
-            downloadButton.setText("Download");
-        }
-
     }//GEN-LAST:event_downloadButtonActionPerformed
 
-    void setComponentsEnabled(boolean enabled){
-        extractButton.setEnabled(enabled);
-        chooseDownloadDirButton.setEnabled(enabled);
-        chooseExtractToButton.setEnabled(enabled);
-        runButton.setEnabled(enabled);
-        platformComboBox.setEnabled(enabled);
-        bundleComboBox.setEnabled(enabled);
-    }
-    
     protected String getDirectoryToDownload(){
         return downloadToTextField.getText();
     }
         jfs.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
         jfs.setAcceptAllFileFilterUsed(false);
         if (jfs.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
-            downloadToTextField.setText(jfs.getSelectedFile().getAbsolutePath());
+            extractToTextField.setText(jfs.getSelectedFile().getAbsolutePath());
         }
     }//GEN-LAST:event_chooseExtractToButtonActionPerformed
 
-    private void extractButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_extractButtonActionPerformed
-        
-    }//GEN-LAST:event_extractButtonActionPerformed
+    private void platformComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_platformComboBoxItemStateChanged
+        if(((String)platformComboBox.getSelectedItem()).equals("Zip"))
+            chooseExtractToButton.setEnabled(true);
+        else 
+            chooseExtractToButton.setEnabled(false);
+    }//GEN-LAST:event_platformComboBoxItemStateChanged
 
     protected void updateDownloadProgressbar(Integer bytes){
         downloadProgressBar.setValue(bytes);
     }
     
+    protected void unzipDownloadedFile (String pathToFileToUnzip){
+        if(!extractToTextField.getText().isEmpty()) {
+            downloadButton.setText("Extracting... Wait");
+            try {
+                int BUFFER = 2048;
+                File file = new File(pathToFileToUnzip);
+
+                ZipFile zip = new ZipFile(file);
+                String newPath = extractToTextField.getText().concat("\\").concat(file.getName().replace(".zip", ""));
+
+                new File(newPath).mkdir();
+                Enumeration zipFileEntries = zip.entries();
+
+                while (zipFileEntries.hasMoreElements())
+                {
+                    ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
+                    String currentEntry = entry.getName();
+                    File destFile = new File(newPath, currentEntry);
+                    File destinationParent = destFile.getParentFile();
+
+                    destinationParent.mkdirs();
+
+                    if (!entry.isDirectory())
+                    {
+                        BufferedInputStream is = new BufferedInputStream(zip
+                        .getInputStream(entry));
+                        int currentByte;
+                        byte data[] = new byte[BUFFER];
+
+                        FileOutputStream fos = new FileOutputStream(destFile);
+                        BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
+
+                        while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
+                            dest.write(data, 0, currentByte);
+                        }
+                        dest.flush();
+                        dest.close();
+                        is.close();
+                    }
+                }
+            } catch (ZipException ex) {
+                Logger.getLogger(NBUpdaterForm.class.getName()).log(Level.SEVERE, null, ex);
+            } catch (IOException ex) {
+                Logger.getLogger(NBUpdaterForm.class.getName()).log(Level.SEVERE, null, ex);
+            }
+            downloadButton.setText("Download");
+        }
+        downloadButton.setEnabled(true);
+    }
+    
     // Variables declaration - do not modify//GEN-BEGIN:variables
     private javax.swing.JComboBox bundleComboBox;
     private javax.swing.JLabel bundleLabel;
     private javax.swing.JProgressBar downloadProgressBar;
     private javax.swing.JLabel downloadToLabel;
     private javax.swing.JTextField downloadToTextField;
-    private javax.swing.JToggleButton extractButton;
     private javax.swing.JLabel extractToLabel;
     private javax.swing.JTextField extractToTextField;
     private javax.swing.JComboBox platformComboBox;
     private javax.swing.JLabel platformLabel;
-    private javax.swing.JToggleButton runButton;
     // End of variables declaration//GEN-END:variables
 
 }