Friday, October 31, 2014

Java Zip File Pro



Extract First File From Zip File Example

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ExtractFileFromZipFile
{
 public static void main(String args[])
{
String sourceZipFile = "C:/FileIO/sourceFile.zip";
try
{
FileInputStream fin = new FileInputStream(sourceZipFile);ZipInputStream zin = new ZipInputStream(fin);ZipEntry entry = zin.getNextEntry();  OutputStream os = new FileOutputStream("c:/extractedFile.css");
byte[] buffer = new byte[1024];
int length;
while( (length = zin.read(buffer)) > 0)
{
os.write(buffer, 0, length);
}                       
os.close(); zin.close();
System.out.println("File Extracted from zip file");
}
catch(IOException e)
{
System.out.println("IOException :" + e);
     }                 
  }
           }

Read int from file using Data Input Stream

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ReadIntFromFile
{
public static void main(String[] args)
{
String strFilePath = "C://FileIO//readInt.txt";
try
{   FileInputStream fin = new FileInputStream(strFilePath);DataInputStream din = new DataInputStream(fin);
int i = din.readInt();
System.out.println("int : " + i);
din.close();
}
catch(FileNotFoundException fe)
{
System.out.println("FileNotFoundException : " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
     }
   }
}

Read byte array from file using DataInputStream

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ReadByteArrayFromFile
{
public static void main(String[] args)
{
 String strFilePath = "C://FileIO//readByteArray.txt";
 try
{
FileInputStream fin = new FileInputStream(strFilePath);
DataInputStream din = new DataInputStream(fin);
byte b[] = new byte[10];
din.read(b);
din.close();     
}
catch(FileNotFoundException fe)
{
System.out.println("FileNotFoundException : " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
     }
   }
}
Read short from file using Data Input Stream

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ReadShortFromFile
 {
public static void main(String[] args)
{
  
String strFilePath = "C://FileIO//readShort.txt";
try
{
FileInputStream fin = new FileInputStream(strFilePath);
      DataInputStream din = new DataInputStream(fin);
                short s = din.readShort();
               System.out.println("short : " + s);
               din.close();
}
catch(FileNotFoundException fe)
{
System.out.println("FileNotFoundException : " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
    }
   }
}

No comments:

Post a Comment