bobrivers2003
Technical User
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);
} 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\\");
}
..
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);
} 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\\");
}
..