Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Unzip a .gz file through code

Posted on 2004-03-25
17
Medium Priority
?
1,151 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

971 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