mailershaven
asked on
Unzipping A File
I've used the script that everyone seems to be pointing to (see below). It works great... if the files are small. If I get a larger file (it won't even take a 1 meg file), I get an error. See the code below and the error.
Does anyone have any suggestions as to other options to implement that are proven? Thanks! I'm on a tight deadline for this..
error:
Object Instantiation Exception.
An exception occurred when instantiating a java object. The cause of this exception was that: .
code:
--------------------
<cfscript>
function unzipFile(zipFilePath, outputPath) {
var zipFile = ""; // ZipFile
var entries = ""; // Enumeration of ZipEntry
var entry = ""; // ZipEntry
var fil = ""; //File
var filOutStream = "";
var bufOutStream = "";
var nm = "";
var pth = "";
var lenPth = "";
zipFile = createObject("java", "java.util.zip.ZipFile");
zipFile.init(zipFilePath);
entries = zipFile.entries();
while(entries.hasMoreEleme nts()) {
entry = entries.nextElement();
if(NOT entry.isDirectory()) {
nm = entry.getName();
lenPth = len(nm) - len(getFileFromPath(nm));
if (lenPth) {
pth = outputPath & left(nm, lenPth);
} else {
pth = outputPath;
}
if (NOT directoryExists(pth)) {
fil = createObject("java", "java.io.File");
fil.init(pth);
fil.mkdirs();
}
filOutStream = createObject(
"java",
"java.io.FileOutputStream" );
filOutStream.init(outputPa th & nm);
bufOutStream = createObject(
"java",
"java.io.BufferedOutputStr eam");
bufOutStream.init(filOutSt ream);
copyInputStream(
zipFile.getInputStream(ent ry),
bufOutStream);
}
}
zipFile.close();
}
function copyInputStream(inStream, outStream) {
var buffer = repeatString(" ",1024).getBytes();
var l = inStream.read(buffer);
while(l GTE 0) {
outStream.write(buffer, 0, l);
l = inStream.read(buffer);
}
inStream.close();
outStream.close();
}
</cfscript>
<cfset unzipFile(expandPath("test .zip"), expandPath("target\"))>
Does anyone have any suggestions as to other options to implement that are proven? Thanks! I'm on a tight deadline for this..
error:
Object Instantiation Exception.
An exception occurred when instantiating a java object. The cause of this exception was that: .
code:
--------------------
<cfscript>
function unzipFile(zipFilePath, outputPath) {
var zipFile = ""; // ZipFile
var entries = ""; // Enumeration of ZipEntry
var entry = ""; // ZipEntry
var fil = ""; //File
var filOutStream = "";
var bufOutStream = "";
var nm = "";
var pth = "";
var lenPth = "";
zipFile = createObject("java", "java.util.zip.ZipFile");
zipFile.init(zipFilePath);
entries = zipFile.entries();
while(entries.hasMoreEleme
entry = entries.nextElement();
if(NOT entry.isDirectory()) {
nm = entry.getName();
lenPth = len(nm) - len(getFileFromPath(nm));
if (lenPth) {
pth = outputPath & left(nm, lenPth);
} else {
pth = outputPath;
}
if (NOT directoryExists(pth)) {
fil = createObject("java", "java.io.File");
fil.init(pth);
fil.mkdirs();
}
filOutStream = createObject(
"java",
"java.io.FileOutputStream"
filOutStream.init(outputPa
bufOutStream = createObject(
"java",
"java.io.BufferedOutputStr
bufOutStream.init(filOutSt
copyInputStream(
zipFile.getInputStream(ent
bufOutStream);
}
}
zipFile.close();
}
function copyInputStream(inStream, outStream) {
var buffer = repeatString(" ",1024).getBytes();
var l = inStream.read(buffer);
while(l GTE 0) {
outStream.write(buffer, 0, l);
l = inStream.read(buffer);
}
inStream.close();
outStream.close();
}
</cfscript>
<cfset unzipFile(expandPath("test
ASKER
Yeah, that didn't help.
I don't care if I have to buy it, I just want something that works and works consistantly! I've done a lot of searching and haven't found anything yet. I'm asking you to point me to places even if they require payment.
Thank you!
I don't care if I have to buy it, I just want something that works and works consistantly! I've done a lot of searching and haven't found anything yet. I'm asking you to point me to places even if they require payment.
Thank you!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
IF YOU HAVE the java SDK (http://java.sun.com/j2se/1.4.2/download.html - not the JRE!) installed and are not afraid to get your hands dirty with some java compilation, you can try this. All the following instructions assume Windows. First, make sure your java compiler can be executed.
Open command prompt and type: javac
If you get something like 'command not found' make sure that the java SDK installation bin directory is in your path (usually something like C:\j2sdk-1.4.2_32\bin).
Then copy the following code to a text file and save as SimpleCFUnzip.java (spelling matters, or not):
---- Start of Java Code -----
import java.io.*;
import java.util.*;
import java.util.zip.*;
public class SimpleCFUnzip {
public boolean unzipFile(String zipFilePath, String outputPath) throws Exception {
ZipFile zipFile = new ZipFile(zipFilePath);
Enumeration entries = zipFile.entries();
ZipEntry entry = null;
String nm = null; // name of the file
File f = null; // folder represented by the path
BufferedOutputStream out = null;
if (!(outputPath.endsWith("/" ) || outputPath.endsWith("/"))) {
outputPath += "/";
}
while(entries.hasMoreEleme nts()) {
entry = (ZipEntry)entries.nextElem ent();
if(!entry.isDirectory()) {
nm = outputPath + entry.getName();
f = new File(nm);
out = new BufferedOutputStream(new FileOutputStream(f));
copyInputStream(zipFile.ge tInputStre am(entry), out);
}
}
zipFile.close();
return true;
}
private void copyInputStream(InputStrea m inStream, OutputStream outStream) throws Exception {
byte[] buf = new byte[102400];
int len;
try {
while((len = inStream.read(buf)) > -1) {
outStream.write(buf, 0, len);
}
} catch (Exception e) {
inStream.close();
outStream.close();
throw e;
} finally {
inStream.close();
outStream.close();
}
}
}
---- End of Java Code -----
Then open a command prompt in the folder where you saved this file and type:
javac -O SimpleCFUnzip.java
Now there should also be a file SimpleCFUnzip.class - copy this file in the \wwwroot\WEB-INF\lib folder of your ColdFusion installation (usually something like C:\CFusionMX\wwwroot\WEB-I NF\lib) and restart ColdFusion.
To use this for unzipping files, this is the CF code:
<cfscript>
unzip = createObject('java', 'SimpleCFUnzip');
unzip.unzipFile( unzipFile , unzipTargetFolder );
</cfscript>
I tested with a 30MB large zip file and it worked fine. Here are the restrictions for this very simple approach:
1) Only handle files in the zip archive that have no folder or path information, just files in the 'root'
2) Performs hardly any error checking whatsoever. It's a good idea to wrap calls to this in a <cftry>-<cfcatch> block and deal with it appropriately
3) It does not create any folders, like the CF version above. You are responsible for all folders to exist
if you want to unzip more than one file, please recreate a new object; that is, do
unzip = createObject('java', 'SimpleCFUnzip');
unzip.unzipFile( unzipFile , unzipTargetFolder );
for each unzip call. In my experience, this is better for memory behavior in the long run.
Open command prompt and type: javac
If you get something like 'command not found' make sure that the java SDK installation bin directory is in your path (usually something like C:\j2sdk-1.4.2_32\bin).
Then copy the following code to a text file and save as SimpleCFUnzip.java (spelling matters, or not):
---- Start of Java Code -----
import java.io.*;
import java.util.*;
import java.util.zip.*;
public class SimpleCFUnzip {
public boolean unzipFile(String zipFilePath, String outputPath) throws Exception {
ZipFile zipFile = new ZipFile(zipFilePath);
Enumeration entries = zipFile.entries();
ZipEntry entry = null;
String nm = null; // name of the file
File f = null; // folder represented by the path
BufferedOutputStream out = null;
if (!(outputPath.endsWith("/"
outputPath += "/";
}
while(entries.hasMoreEleme
entry = (ZipEntry)entries.nextElem
if(!entry.isDirectory()) {
nm = outputPath + entry.getName();
f = new File(nm);
out = new BufferedOutputStream(new FileOutputStream(f));
copyInputStream(zipFile.ge
}
}
zipFile.close();
return true;
}
private void copyInputStream(InputStrea
byte[] buf = new byte[102400];
int len;
try {
while((len = inStream.read(buf)) > -1) {
outStream.write(buf, 0, len);
}
} catch (Exception e) {
inStream.close();
outStream.close();
throw e;
} finally {
inStream.close();
outStream.close();
}
}
}
---- End of Java Code -----
Then open a command prompt in the folder where you saved this file and type:
javac -O SimpleCFUnzip.java
Now there should also be a file SimpleCFUnzip.class - copy this file in the \wwwroot\WEB-INF\lib folder of your ColdFusion installation (usually something like C:\CFusionMX\wwwroot\WEB-I
To use this for unzipping files, this is the CF code:
<cfscript>
unzip = createObject('java', 'SimpleCFUnzip');
unzip.unzipFile( unzipFile , unzipTargetFolder );
</cfscript>
I tested with a 30MB large zip file and it worked fine. Here are the restrictions for this very simple approach:
1) Only handle files in the zip archive that have no folder or path information, just files in the 'root'
2) Performs hardly any error checking whatsoever. It's a good idea to wrap calls to this in a <cftry>-<cfcatch> block and deal with it appropriately
3) It does not create any folders, like the CF version above. You are responsible for all folders to exist
if you want to unzip more than one file, please recreate a new object; that is, do
unzip = createObject('java', 'SimpleCFUnzip');
unzip.unzipFile( unzipFile , unzipTargetFolder );
for each unzip call. In my experience, this is better for memory behavior in the long run.
var buffer = repeatString(" ", 102400).getBytes();
function copyInputStream(inStream, outStream) {
var l = inStream.read(buffer);
while(l GTE 0) {
outStream.write(buffer, 0, l);
l = inStream.read(buffer);
}
inStream.close();
outStream.close();
}
I have implemented a high-performance Java-based tool for unzipping and creating huge archives. But it is for sale and I don't know what policies EE has about posting such links. I highly suggest you look around what's available and spend the few dollars others charge for this stuff.