2015-02-06 35 views
-2

在我的應用程序中,我想壓縮來自給定用戶輸入的文件或文件夾。當我嘗試從JDialog獲得輸入時,這些工作正常,但如果我想嘗試讓用戶從fileChooser中選擇,我的程序將無法工作 - 它始終創建一個空的zip文件。請你能教我如何解決這個問題嗎?從JFileChooser給出的字符串的文件名不會工作

編輯:當我嘗試通過JDialog獲取文件名和輸出名時,這工作正常,但是當我想通過filechooser選擇文件名時,我無法將其傳遞給我的更多功能正確的方式。也許這可能是因爲目錄分隔符?它寫入文件名和文件路徑,但當我通過它不會工作。

import java.io.*; 
import java.util.zip.*; 
import javax.swing.*; 
import javax.swing.JFileChooser; 

public class Zipper { 

int prefixLength; 
    ZipOutputStream zipOut; 
    byte[] ioBuffer = new byte[4096]; 

    public Zipper(String dirFileName, String dirFileOutput) throws Exception 
    { prefixLength = dirFileName.lastIndexOf("/") + 1; 
    zipOut = new ZipOutputStream(new FileOutputStream("./" + dirFileOutput + ".zip")); 
    createZipFrom(new File(dirFileName)); 
    zipOut.close(); 
    } 

    void createZipFrom(File dir) throws Exception 
    { if (dir.exists() && dir.canRead() && dir.isDirectory()) 
    { File[] files = dir.listFiles(); 
     if (files != null) 
     { for (File file: files) 
     { if (file.isDirectory()) 
      { createZipFrom(file); 
      } 
      else 
      { String filePath = file.getPath();//.replace('\\', '/'); 
      FileInputStream in = new FileInputStream(filePath); 
      zipOut.putNextEntry(new ZipEntry(filePath.substring(prefixLength))); 
      int bytesRead; 
      while ((bytesRead = in.read(ioBuffer)) > 0) 
      { zipOut.write(ioBuffer, 0, bytesRead); 
      } 
      System.out.println(filePath + " added\n"); 
      zipOut.closeEntry(); 
      in.close(); 
      } 
     } 
     } 
    } 
    } 

    public static void main(String[] args) throws Exception { 

    JFileChooser fileChooser = new JFileChooser(); 
    fileChooser.showOpenDialog(null); 
    File selectedFile = fileChooser.getSelectedFile(); 
    System.out.println(selectedFile.getPath()); 

    String dirFileName = selectedFile.getPath(); // should come from the fileChooser but isnt working 

    String dirFileOutput = JOptionPane.showInputDialog(null, "packetname"); // thats working.. 

    System.out.println(dirFileName); 
    System.out.println(dirFileOutput); 

    new Zipper(dirFileName, dirFileOutput); 

    System.out.println("package " + dirFileOutput + "." + ".zip created\n"); 

    } 

} 

編輯:我得到了它不斷變化的

prefixLength = dirFileName.lastIndexOf("/") + 1; 

這個

prefixLength = dirFileName.lastIndexOf("\\") + 1; 
+0

這是什麼應該這樣做,它實際上做了什麼呢? – immibis 2015-02-06 11:00:15

+1

所以你說的是「它應該工作,而不是它的工作」 – immibis 2015-02-06 11:01:46

+0

權利,現在呢?如果我知道更多,我wouldnt問..但我嘗試編輯我的問題。讓我們看看 – BrainWorx 2015-02-06 11:03:06

回答

2

你不檢查返回值運行。 請閱讀JFileChooserDialog javadoc

JFileChooser爲用戶選擇 文件的簡單機制。有關使用JFileChooser的信息,請參閱如何使用文件 選擇器,The Java Tutorial中的一部分。下面的代碼會彈出用戶的主目錄 文件選擇器,只看到.gif圖片jpg和 :

JFileChooser chooser = new JFileChooser(); 
FileNameExtensionFilter filter = new FileNameExtensionFilter(
    "JPG & GIF Images", "jpg", "gif"); 
chooser.setFileFilter(filter); 
int returnVal = chooser.showOpenDialog(parent); 
if(returnVal == JFileChooser.APPROVE_OPTION) { 
    System.out.println("You chose to open this file: " + 
     chooser.getSelectedFile().getName()); 
} 

下面的代碼工作對我來說:

 // ... 
     JFileChooser fileChooser = new JFileChooser(); 
     int result = fileChooser.showOpenDialog(appFrame); 
     if (result == JFileChooser.APPROVE_OPTION) { 
      File selectedFile = fileChooser.getSelectedFile(); 
      System.out.println(selectedFile.getPath()); 

      String dirFileName = selectedFile.getPath(); // should come from the fileChooser but isnt working 

      String dirFileOutput = JOptionPane.showInputDialog(null, "packetname"); // thats working.. 

      System.out.println(dirFileName); 
      System.out.println(dirFileOutput); 

      System.out.println("package " + dirFileOutput + "." + ".zip created\n"); 
     } 
+0

謝謝!這一分鐘,我試圖如果(結果== JFileChooser.APPROVE_OPTION){功能,並已看到它的作品。萬分感謝! – BrainWorx 2015-02-06 11:16:38

+0

它不適合我這種方式,但正確的答案是檢查返回值。謝謝 – BrainWorx 2015-02-06 11:29:39

+0

嗯,但無論如何,文件名似乎無法正確傳遞,因爲創建的zip文件夾是空的。 – BrainWorx 2015-02-06 11:56:48