hallo, ich habe hier ein programm, welches zipfiles entzippen soll. im prinziptut es das auch, aber manchmal kommt eine FileNotFoundException in der kommentierten zeile. aber warum? wenns manchmal geht, dann sollte es doch immer gehen? also hier mal der code:
man muß das programm mit einem zipfile starten, also:
java UnZip file.zip
manchmal gehts und manchmal nicht, also am besten probieren, bis es mal nicht geht. aber vielleicht seht ihr ja den fehler auch so.
DANKE
Code:
//java UnZip zipfile --> extract all
//java UnZip zipfile file --> extract the given file
import java.io.*;
import java.util.*;
import java.text.*;
import java.util.zip.*;
class UnZip {
public static void main(String args[]) throws IOException {
//get the zipfile from the first commandline parameter
InputStream in = new BufferedInputStream(new FileInputStream(args[0]));
ZipInputStream zin = new ZipInputStream(in);
ZipEntry zippy;
boolean success, create=true;
String fs = System.getProperty("file.separator");
//This must be replaced by a JFileChooser dialog!
String dir= "c:"+fs+"jpack";
if(create){
//try to create the directory
success = (new File(dir)).mkdirs();
if (!success) {
//Directory creation failed or dir already exists
System.out.println("Dir exists or writing error!");
}
}
// as long as there is more data inside the zipfile read it
while((zippy=zin.getNextEntry())!= null) {
//extract only one given file?
if (args.length > 1) {
//given file found? --> extract it
if (zippy.getName().equals(args[1])) {
unzip(zin, args[1], dir);
break;
}
}else{
//extract all files
unzip(zin, zippy.getName(), dir);
}
}
zin.close();
}
/**
* Unzips a given zipfile
*
* @param ZipInputStream zin: Reads a zip file
* @param String s: The zipfile
* @param String dir: The destiniation dir for th content of the zipfile
*/
public static void unzip(ZipInputStream zin, String s, String dir) throws IOException {
String fs = System.getProperty("file.separator");
File f = new File(s);
//Are there any subdirs inside the zip?
if(f.getParent() != null){
//Create the subdirs
//example: c:\programme\subdir
File nd = new File(dir + fs + f.getParent());
//System.out.println(nd);
System.out.println("creating: " + dir + fs + f.getParent());
nd.mkdirs();
}
//System.out.println("unzipping " + s + " to: " + dir + "...");
//System.out.println("path: " + dir+fs+s);
String p=dir+fs+s;
//String p= dir+fs+f.getName();
System.out.println("p: " + p);
//hier bringt er manchmal die exception?????
FileOutputStream out = new FileOutputStream(p);
byte [] b = new byte[512];
int len = 0;
while ( (len=zin.read(b))!= -1 ) {
out.write(b,0,len);
}
out.close();
}
}
man muß das programm mit einem zipfile starten, also:
java UnZip file.zip
manchmal gehts und manchmal nicht, also am besten probieren, bis es mal nicht geht. aber vielleicht seht ihr ja den fehler auch so.
DANKE