We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Unzip a .gz file through code

jvosika
jvosika asked
on
Medium Priority
1,176 Views
Last Modified: 2013-12-24
Could someone tell me how to unzip a .gz file with Cold Fusion if it is possible at all?

Thank you!

Jim Vosika
http://www.wallclocksrus.com
Comment
Watch Question

Hi Jim,

I am fairly certain that there is a Java component out there somewhere that you can extend (you're using CFMX, right?)to do this, but I was unable to hunt it down, so here's a quick solution you might try:

Download gzip  for DOS (it's free), and after extracting the files, place gzip.exe in a folder outside your webroot.
(get it here, remember the DOS version) http://www.gzip.org/#exe

Then execute gzip to extract the file:
<cfexecute name="c:\gzip\gzip.exe" arguments="-d c:\temp\mygzipfile.gz" outputfile="c:\gzip\log.txt">
</cfexecute>

That should get you started, let me know if you have any more questions!

Take Care,

Seth
Hey
After Research
I just got a code for u ,it might heip


 Unzip a file in CFMX with java.util.zip
Posted by Sam at July 28, 2003 09:21 PM
The question of unzipping a file came up on the forums again. I've always replied that they can use CFX_Zip in CF5 and the java.util.zip package in CFMX. Never having had to actually use this myself in a ColdFusion application, I never relalized that java.util.zip is not all that straightforward.

After some research and one server crash, I put together this UDF.

     
<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\"))>

Regards
Sandy

 

Author

Commented:
Sandy,
  I copied out the code exacylt and placed the name of my file in there like so:
    <cfset unzipFile(expandPath("test.txt.gz"), expandPath("target\"))>
  and the code threw errors on this like:
    zipFile.init(zipFilePath);

So I tried using a normal zip file (*.zip) with one file in it and it made it past zipFile.init(zipFilePath); but threw an error on the next line:
    entries = zipFile.entries();

I also tried renaming test.txt.gz to test.gz and it still threw an error at entries = zipFile.entries();

Do you have any ideas what is causing this error? Is there something in the CF Administrator that needs to be changed for this to work right?

Thanks!
Jim Vosika
Hi Jim,

gZip is a different file format from Zip, so unfortunately the tried and true unzipping methods for zip files will not do what you're needing to do.

Take Care,

Seth

Commented:
You can use the SDK from Stuffit and do this on a Linux box Go here:
http://www.stuffit.com/unix/
or
http://www.stuffit.com/win/sdk/index.html

Regards

Commented:
If your site is PHP enabled, just use this script:
<?php

$zip = zip_open("/tmp/test2.zip");

if ($zip) {

   while ($zip_entry = zip_read($zip)) {
       echo "Name:              " . zip_entry_name($zip_entry) . "\n";
       echo "Actual Filesize:    " . zip_entry_filesize($zip_entry) . "\n";
       echo "Compressed Size:    " . zip_entry_compressedsize($zip_entry) . "\n";
       echo "Compression Method: " . zip_entry_compressionmethod($zip_entry) . "\n";

       if (zip_entry_open($zip, $zip_entry, "r")) {
           echo "File Contents:\n";
           $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
           echo "$buf\n";

           zip_entry_close($zip_entry);
       }
       echo "\n";

   }

   zip_close($zip);

}

?>

Commented:
one more solution is to create a UDF:

UDF:
<cfscript>
/**
 * Unzips a file to the specified directory.
 *
 * @param zipFilePath        Path to the zip file (Required)
 * @param outputPath        Path where the unzipped file(s) should go (Required)
 * @return void
 * @author Samuel Neff (sam@serndesign.com)
 * @version 1, September 1, 2003
 */
function unzipFile(zipFilePath, outputPath) {
      var zipFile = ""; // ZipFile
      var entries = ""; // Enumeration of ZipEntry
      var entry = ""; // ZipEntry
      var fil = ""; //File
      var inStream = "";
      var filOutStream = "";
      var bufOutStream = "";
      var nm = "";
      var pth = "";
      var lenPth = "";
      var buffer = "";
      var l = 0;
     
      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);
            
            inStream = zipFile.getInputStream(entry);
            buffer = repeatString(" ",1024).getBytes();
            
            l = inStream.read(buffer);
            while(l GTE 0) {
                  bufOutStream.write(buffer, 0, l);
                  l = inStream.read(buffer);
            }
            inStream.close();
            bufOutStream.close();
            }
      }
      zipFile.close();
}
</cfscript>

File info:
unzipFile(zipFilePath, outputPath)
Author: Samuel Neff (Send Email)
Library/Category: FileSysLib/File
Required CF Version: ColdFusion MX
Version: 1
Last Updated: September 1, 2003
Rating: 4.00 out of 5 based on 5 ratings.

Description
unzipFile() utilizes the built in java.util.zip package and requires no software be installed on the server. Pass in the path to a zip file and a target directory and it will unzip the contents.

Due to a bug in CFMX 6.0 the UDF does require CFMX 6.1.

Parameters
Name Description Required
zipFilePath Path to the zip file Yes
outputPath Path where the unzipped file(s) should go Yes


Return Values
void

Example
<!---

unzipFile(expandPath("test.zip"), expandPath("targetDir/"))

--->

Author

Commented:
ekriner,
   None of these solutions seem to work with .gz files, just .zip's are working. Is there anyway to modify them to handle .gz files?

Thanks!
Jim

Author

Commented:
Seth,
   I finally had a chance to download and try using cfexecute to run gzip.exe and sometimes I get this error which I have never seen before:

Index: 76, Size: 26

Other times it doesnt throw and error but outputs a blank file.

Any suggestions?

-Jim
Hi Jim,

Unfortunately, I won't be in a position to play with the code today (we are moving into our noew home!).

Since I haven't tested the code first-hand, I can't really make any competent suggestions.  Are you using the code exactly as I provided it, with only the filename changed?

If you'll provide the code in the context you are using it, I'll be happy to see if anything jumps out at me. :)

Wish I could help more.  If you don't have this figured out by Monday, I'll have a look and see if I can't get it working, or at least make other suggestions.

Take Care,

Seth
Jim,

Just a thought - read the docs for gzip and try toying with the command-line parameters.  You need -d for sure, but there is a laundry list of other params available that you can play with also.

Seth

Author

Commented:
still no luck after quite a while of messing with it, i am starting to wonder if it cant be done... :(
inorder to work with Gzip files you need to use the GZIPOutputStream..I will post code in a while...but it is definately possible.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
any luck? I tested it out today and it looked to work exactly how you wanted it to work. Please let me know if you need it changed at all!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.