Solved

Unzip a .gz file through code

Posted on 2004-03-25
17
1,135 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
0
Comment
Question by:jvosika
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +2
17 Comments
 
LVL 5

Expert Comment

by:Seth_Bienek
ID: 10684718
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
0
 
LVL 4

Expert Comment

by:sandy12879
ID: 10686220
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

 
0
 

Author Comment

by:jvosika
ID: 10686729
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
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 5

Expert Comment

by:Seth_Bienek
ID: 10687388
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
0
 

Expert Comment

by:ekriner
ID: 10690426
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
0
 

Expert Comment

by:ekriner
ID: 10690443
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);

}

?>
0
 

Expert Comment

by:ekriner
ID: 10690515
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/"))

--->

0
 

Author Comment

by:jvosika
ID: 10691712
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
0
 

Author Comment

by:jvosika
ID: 10691814
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
0
 
LVL 5

Expert Comment

by:Seth_Bienek
ID: 10694999
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
0
 
LVL 5

Expert Comment

by:Seth_Bienek
ID: 10695009
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
0
 

Author Comment

by:jvosika
ID: 10703656
still no luck after quite a while of messing with it, i am starting to wonder if it cant be done... :(
0
 
LVL 6

Expert Comment

by:PE_CF_DEV
ID: 11017725
inorder to work with Gzip files you need to use the GZIPOutputStream..I will post code in a while...but it is definately possible.
0
 
LVL 6

Accepted Solution

by:
PE_CF_DEV earned 500 total points
ID: 11018256
This should give you a good start. I don't have a computer to test it on right now but this should give you a good idea on how to use the GZipInputStream. This code was oroginally posted here: http://javaalmanac.com/egs/java.util.zip/CompArray.html?l=rel 

I adapted it to work in coldfusion.

<cfscript>
try {
        // Open the compressed file
        inFilename = "C:\Inetpub\wwwroot\test.txt.gz";
            file_in = createobject('java','java.io.FileInputStream').init(inFilename);
            fin = createobject('java','java.util.zip.GZIPInputStream').init(file_in);
   
        // Open the output file
        outFilename = "C:\Inetpub\wwwroot\test2.txt";
        out = createobject('java','java.io.FileOutputStream').init(outFilename);
              safecount = 1;
        buf =  repeatString(" " ,100).getBytes();
            flen = fin.read(buf);
        while (flen GT 0) {
            out.write(buf, 0, flen);
                  flen = fin.read(buf);
        }
        // Close the file and stream
            file_in.close();
        fin.close();
        out.close();
            
    } catch (IOException e) {
    }
</cfscript>
0
 
LVL 6

Expert Comment

by:PE_CF_DEV
ID: 11054314
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!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help Fix A 403 error 13 125
wordpress limitations 4 148
Is it true tt IIS7 can't support TLSv1.2 if OS is on Win2008 1 84
SSL Certificate for IIS7 Site 2 118
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question