Snehal Kulkarni
Snehal Kulkarni

Reputation: 105

How to check whether file is gzip or not in Java

How to check whether file is gzip or not in java. I checked by reading first 2 bytes and comparing with magic code. But for large size of file getting OutOfMemoryError.

Any one knows other way to do this?

This is the code I am using:

def isGzipCompressionFile(File file)
{
   return ((file.bytes[0] == (byte) (GZIPInputStream.GZIP_MAGIC)) && (file.bytes[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8)))
}

Upvotes: 8

Views: 16419

Answers (5)

Peter Paul Kiefer
Peter Paul Kiefer

Reputation: 2124

Use a gzip input stream http://docs.oracle.com/javase/7/docs/api/java/util/zip/GZIPInputStream.html . It throws an ZipException if you try to open another format. In your code you can catch this exception in a catch block.

Upvotes: 4

Ajay Sainy
Ajay Sainy

Reputation: 379

This is what I am using

private static void decompressGzipFile(String gzipFilePath, String newFilePath) {
        try {
            FileInputStream fis = new FileInputStream(gzipFile);
            GZIPInputStream gis = new GZIPInputStream(fis);
            // If this line does not throw exception your file is GZip
            // Your logic


        } catch (IOException e) {
            //Not in GZip Format
        }

    }

Upvotes: 1

Bacteria
Bacteria

Reputation: 8596

Try Files.probeContentType(Path) [JDK 7]

Path source = Paths.get("D:/myfiles/a.zip");
System.out.println(Files.probeContentType(source));

output

application/x-zip-compressed

Upvotes: 6

Nicholas Hirras
Nicholas Hirras

Reputation: 2596

You should only read in 2 bytes from the file if that's all you're checking, it sounds like you're pulling the entire file into memory.

https://docs.oracle.com/javase/tutorial/essential/io/datastreams.html

Upvotes: 1

Luke Rixson
Luke Rixson

Reputation: 647

Use this package that I found on google:

package example;
 
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.zip.GZIPInputStream;
 
public class GZipUtil {
 
 /**
  * Checks if an input stream is gzipped.
  * 
  * @param in
  * @return
  */
 public static boolean isGZipped(InputStream in) {
  if (!in.markSupported()) {
   in = new BufferedInputStream(in);
  }
  in.mark(2);
  int magic = 0;
  try {
   magic = in.read() & 0xff | ((in.read() << 8) & 0xff00);
   in.reset();
  } catch (IOException e) {
   e.printStackTrace(System.err);
   return false;
  }
  return magic == GZIPInputStream.GZIP_MAGIC;
 }
 
 /**
  * Checks if a file is gzipped.
  * 
  * @param f
  * @return
  */
 public static boolean isGZipped(File f) {
  int magic = 0;
  try {
   RandomAccessFile raf = new RandomAccessFile(f, "r");
   magic = raf.read() & 0xff | ((raf.read() << 8) & 0xff00);
   raf.close();
  } catch (Throwable e) {
   e.printStackTrace(System.err);
  }
  return magic == GZIPInputStream.GZIP_MAGIC;
 }
 
 public static void main(String[] args) throws FileNotFoundException {
  File gzf = new File("/tmp/1.gz");
 
  // Check if a file is gzipped.
  System.out.println(isGZipped(gzf));
 
  // Check if a input stream is gzipped.
  System.out.println(isGZipped(new FileInputStream(gzf)));
 }
}

Upvotes: 8

Related Questions