Link to home
Start Free TrialLog in
Avatar of goldentine
goldentine

asked on

Compress in GZIP

I have this function to Unzip Gzip compressed files.  What I need is to be able to compress files in the Gzip format.

Using this function as an example, how could I rewrite it to compress in to a "*.gz" file?  Thanks in advance.

/**
 * Unzip a gzipped (.gz) file.
 * @param infilePath        Full path to gz file. (Required)
 * @param outputPath        Where to extract the gzfile. Defaults to current directory. (Optional)
 * @return Returns a boolean.
 */
function gunzipFile(infilePath) {
      var zipfile = "";
      var outfile = "";
      var outputPath = "";
      var infile = "";
      var gzInStream = createObject('java','java.util.zip.GZIPInputStream');
      var outStream = createObject('java','java.io.FileOutputStream');
      var inStream = createObject('java','java.io.FileInputStream');
      var buffer = repeatString(" ",1024).getBytes();
      var length = 0;
      var rv = true;
      var e = "";
   
      if (arrayLen(Arguments) gte 2) outputPath = arguments[2];
      else outputPath = getDirectoryFromPath(infilePath);
      if (right(infilePath,3) neq '.gz') infilePath = infilePath & '.gz';
      if(right(outputPath,1) neq "/" and right(outputPath,1) neq "\") outputPath = outputPath & "/";

   
      try {
            infile = getFileFromPath(infilePath);
            outfile = outputPath & left(infile,len(infile) - 3);
            inStream.init(infilePath);
            gzInStream.init(inStream);
            outStream.init(outfile);
            do {
                  length = gzInStream.read(buffer,0,1024);
            if (length neq -1) outStream.write(buffer,0,length);
            } while (length neq -1);
            outStream.close();
            gzInStream.close();
      }
      catch (any e) {
            rv = false;
            try {
                  outStream.close();
            } catch (any e) { }
                  try {
                        gzInStream.close();
                  } catch (any e) { }
      }
      return rv;
}
ASKER CERTIFIED SOLUTION
Avatar of pinaldave
pinaldave
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of goldentine
goldentine

ASKER

Thanks, I found a solution.