Difference between revisions of "Gzip and gunzip a file"

From CodeCodex

(Created page with "The following code samples show how to gzip and gunzip files. ==Implementations== ===Java=== <pre class="java"> import java.util.zip.*; import java.io.*; </pre> <pre class="jav...")
 
(Java)
 
Line 2: Line 2:
 
==Implementations==
 
==Implementations==
 
===Java===
 
===Java===
 +
Use these imports:
 
<pre class="java">
 
<pre class="java">
 
import java.util.zip.*;
 
import java.util.zip.*;
 
import java.io.*;
 
import java.io.*;
 
</pre>  
 
</pre>  
 
+
And here is the method to unzip a file.
 
<pre class="java">
 
<pre class="java">
 
public static String Unzip(String inFilePath) throws Exception
 
public static String Unzip(String inFilePath) throws Exception

Latest revision as of 23:15, 17 February 2012

The following code samples show how to gzip and gunzip files.

Implementations[edit]

Java[edit]

Use these imports:

import java.util.zip.*;
import java.io.*;

And here is the method to unzip a file.

public static String Unzip(String inFilePath) throws Exception
{
    GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(inFilePath));
 
    String outFilePath = inFilePath.replace(".gz", "");
    OutputStream out = new FileOutputStream(outFilePath);
 
    byte[] buf = new byte[1024];
    int len;
    while ((len = gzipInputStream.read(buf)) > 0)
        out.write(buf, 0, len);
 
    gzipInputStream.close();
    out.close();
 
    new File(inFilePath).delete();
 
    return outFilePath;
}