Reply to topic
Unzip a file in CFMX with java.util.zip
tobiansj
HostMySite Supervisor

Joined: 28 Jan 2004
Posts: 6
Location: Newark, DE.
Reply with quote
This information was taken from www.rewindlife.com

http://www.rewindlife.com/archives/000041.cfm


<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.hasMoreElements()) {
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(outputPath & nm);

bufOutStream = createObject(
"java",
"java.io.BufferedOutputStream");

bufOutStream.init(filOutStream);

copyInputStream(
zipFile.getInputStream(entry),
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\"))>


It accepts the full path of the zip file to unzip and the target path to extract to. The target path must end in a '\' or '/'.
Unzip a file in CFMX with java.util.zip
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic