我试图用Java将Windows计算机上的文件解压缩。当我从main()方法调用unZip()方法时,它工作正常,但是当我将此代码部署到服务器时,它抛出异常,提示FileNotFoundException(系统找不到指定的路径)。谁能帮助我。
public static void unZip(String zipFilePath, String destDirectory) {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipInputStream zipIn=null;
try {
zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
extractFile(zipIn, filePath);
} else {
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
} catch (Exception e) {
System.out.println("exception occurred while Unzipping "+e);
}finally{
try{
if(zipIn!=null){
zipIn.close();
}
}catch(IOException ioe){
System.out.println("exception occureed while closing the stream"+ioe);
}
}
}
private static void extractFile(ZipInputStream zipIn, String filePath) {
BufferedOutputStream bos=null;
FileOutputStream fos=null;
try {
if(filePath.contains("/")){
filePath = filePath.replace("/", "\\");
}
fos=new FileOutputStream(filePath);
bos = new BufferedOutputStream(fos);
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
} catch (Exception e) {
System.out.println("exception occurred while extracting"+e);
}finally{
try{
if(fos!=null){
fos.close();
}
if(bos!=null){
bos.close();
}
}catch(IOException ioe){
System.out.println("exception occureed while closing the stream"+ioe);
}
}
}
注意:在两种情况下我都使用相对路径。