Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem extracting .jar and .zip with directory structure

Status
Not open for further replies.

bobrivers2003

Technical User
Oct 28, 2005
96
GB
I have created a class that extracts all files within a built EAR application, within this EAR there would be additional archives that exist that also would need extracting.

I am having trouble extracting jars and zip files that have files in multiple level of directories e.g. dir/dir1/dir2/fileName. For these it returns java.io.FileNotFoundException "path and file name" (The system cannot find the path specified).

I discovered that this is because the directory is not being created before the file is extracted. But I have the code to do this and it working for wars and ears of all directory level types. Just zips and jars being a pain.

If anyone can shed some light will be may savior as a week of head banging is begining to hurt!!!

Any help would be greatly appreciated :)

...
public static final void copyInputStream(InputStream in, OutputStream out)
throws IOException {
byte[] buffer = new byte[1024];
int len;

while ((len = in.read(buffer)) >= 0)
out.write(buffer, 0, len);

in.close();
out.close();
}

...

// Extract zip directory
public void getZipFiles(String filename, String tempDir) {
Enumeration entries;
ZipFile zipFile;
checkDir(tempDir, true);//Check if dir exists, if it does it deletes contents, if not it create a new dir

try {
zipFile = new ZipFile(filename);
String destinationname = tempDir;
entries = zipFile.entries();


while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry)entries.nextElement();


if (entry.isDirectory()) {
// Assume directories are stored parents first then children.


System.out.println("Extracting [directory]: " + entry.getName());

(new File(destinationname + entry.getName())).mkdir();

System.out.println(destinationname + entry.getName());
continue;

}


if (entry.getSize() > 0) {


System.out.println("Extracting [file]: " + entry.getName());

try {


copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(
new FileOutputStream(destinationname + entry.getName())));

}catch (Exception i){


System.err.println(i);
}

String fileExtention =
entry.getName().substring(entry.getName().toString().lastIndexOf(".") + 1);
String fileNameNoExt =
entry.getName().substring(0,entry.getName().lastIndexOf("."));
String justDirName =
entry.getName().substring(fileNameNoExt.length());



// Add file extensions to hash table
// to create record of file types in archive
if (fileExt.containsKey(fileExtention)) {

Integer num = (Integer) fileExt.get(fileExtention);
int n = num.intValue() + 1;
fileExt.put(fileExtention, new Integer(n));

} else {


fileExt.put(fileExtention, new Integer(1));

}

if(fileExtention.equalsIgnoreCase("war")) {

String extractDir = tempDir + fileNameNoExt + "\\";
checkDir(extractDir, true);
System.out.println("Extracting [WAR]: " + entry.getName());
getZipFiles(tempDir, + entry.getName(), extractDir);
}

// if (fileExtention.equalsIgnoreCase("jar")) {
//
// String extractDir = tempDir + fileNameNoExt + "\\";
// System.out.println("EXTRACT DIR " + extractDir);
// System.out.println("TEMP DIR " + tempDir);
// System.out.println("fileNameNoExt " + fileNameNoExt);
//
// checkDir(extractDir, true);
//
// System.out.println("Extracting [JAR]: " + entry.getName());
// getZipFiles(tempDir + entry.getName(), extractDir);
//
// }


} else {

System.err.println("[ZERO BYTE OBJECT]: " + entry.getName());
zeroFileCount++;
}



}

zipFile.close();

} catch (IOException ioe) {
System.err.println("Unhandled exception:");
ioe.printStackTrace();
return;
}
}

...

public static void main(String[] args){

Unzip unzip = new Unzip();
unzip.getZipFiles("C:\\FILE.jar\\", "C:\\tempDIR\\");

}

..
 
Just a guess; might be easier to use recursion here. Create a method to extract files from a directory and use the method to process the the extracted files recursively

public void extract(String dir){
try{
File f = new File(dir);
if(f.exists() && f.canRead() && f.isDirectory()){
String[] files = f.list();
for(int j = 0;j<files.length;j++){
extract(files[j]);
}
}
}catch(Exception e){ e.printStackTrace(); }
}
You may have to substitute the code above with the zip extraction definition as you require.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top