在我的应用程序中,我想从给定的用户输入中压缩文件或文件夹。当我尝试从JDialog获取输入时,这很好但是如果我想让用户从fileChooser中选择,我的程序将不起作用 - 它总是创建一个空的zip文件。请问你能教我如何解决这个问题吗?
编辑:当我尝试通过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 :(得分:2)
您不检查返回值。 请阅读JFileChooserDialog javadoc:
JFileChooser为用户提供了一种简单的机制来选择 文件。有关使用JFileChooser的信息,请参阅如何使用文件 选择器,The Java Tutorial中的一个部分。弹出以下代码: 用户主目录的文件选择器,只能看到.jpg和 .gif图片:
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");
}