Search Java Programs

Friday, February 5, 2010

Zipping a File

This example shows how to create a zip file in java. In other words, we will learn how to compress a file just to take less space. We can compress any file using various standalone applications. These standalone applications are nothing but the zip (compress or decompress )tools. Examples of zip tools are Winrar, WinZip etc. It is also possible to zip and unzip the files from your Java applications. This example shows how we zip a file through a java program.
The java.util.zip.DeflaterOutputStream class is immediate supper class of java.util.zip.ZipOutputStream class. java.util.zip. ZipOutputStream implements java.util.zip.ZipConstants interface. The class ZipOutStream implements an output stream filter to write files in the zip file format. This class can be used for both compressed and uncompressed files.

The java.io.FilterOutputStream class extends java.io.BufferedOutputStream class and implements a buffered output stream. We can write a byte steam into a file on the system. The java.io.FileInputStream class extends java.io.InputStream class. FileInputStream class is used to obtain input bytes from a file in a file system. The FileInputStream class is used to read stream of row bytes (such as image data), for reading streams of characters by considering FileReader class.

putNextEntry(new ZipEntry(filesToZip)):

This is the method of ZipOutputStream class which is used to enter files one by one to be zipped. This method is used to close previous zip entry if any active and create the next zip entry by passing the instance of the ZipEntry class which holds the file name to be zipped. In this example we are going to zip "profile.txt" into "outFile.zip" .To execute this example you first need to create profile.txt in same directory.


Here is the code of the program : 
 

import java.io.*;
import java.util.zip.*;
public class ZipFileExample
  {
public static void main(String a[])
  {
  try
    {
    ZipOutputStream out = new ZipOutputStream(new 
BufferedOutputStream(new FileOutputStream("outFile.zip")));
    byte[] data = new byte[1000];               
    BufferedInputStream in = new BufferedInputStream
(new FileInputStream("profile.txt"));
    int count;
   out.putNextEntry(new ZipEntry("outFile.zip"));
   while((count = in.read(data,0,1000)) != -1)
   {      
   out.write(data, 0, count);
   }
   in.close();
   out.flush();
   out.close();
  System.out.println("Your file is zipped");
 }
 catch(Exception e)
  {
 e.printStackTrace();
  }        
 }
}
Download this example.

Convert InputStream to File

Here we are showing how to convert an InputStream to File.
To do so first read the file as InputStream  using FileInputStream. Create FileOutputStream class object to retrieve the file from system for modification then convert the InputSteam into byte array before writing into file then pass this array to the input stream and finally the byte array into file. Now all the data  from InputStream is written into the desired file.


The code of the program is given below:


import java.io.*;
public class InputStreamToFile
  {
  public static void main(String args[])
    {
    try
    {
    File f=new File("outFile.java");
    InputStream inputStream= new FileInputStream
("InputStreamToFile.java");
    OutputStream out=new FileOutputStream(f);
    byte buf[]=new byte[1024];
    int len;
    while((len=inputStream.read(buf))>0)
    out.write(buf,0,len);
    out.close();
    inputStream.close();
    System.out.println("\nFile is created........
...........................");
    }
    catch (IOException e){}
    }
  }
 
The output of the program is given below:
C:\rajesh\io>javac InputStreamToFile.java
C:\rajesh\io>java InputStreamToFile
File is created.......................

Convert Inputstream to OutputStream

This discussion shows how to convert an  InputStream to OutputStream. Here we are trying to make understand the conversion of an input stream into an output stream by taking an example. In this example we are taking a file as input stream and also creating an object of output stream. To convert an input stream into output stream we read a single character from input stream and write it into an output stream.
The class  java.io.InputStream is an abstract class which extends Object class and is a superclass of all classes that represents an input stream of byte.
.
The class java.io.OutputStream is an abstract class which extends Object class. java.io.OutputStream class is superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends the output to destination.

The class java.io.FileInputStream  is an abstract class which extends InputStream. A FileInputStream calss is used to obtain input bytes from a file in a file system.

System class contains several useful classes and methods and can never be instantiated. This class extends Object class.

Field Summary

System.err: "err" is used to display error output stream.

InputStream.in: "in" is used to tack input from input stream.

PrintStream.out: out is used for displaying the standard output stream.

The code of the program is given below:


import java.io.*; 
public class InputToOutputStream
{
   public static void main( String args[] )
throws IOException
     {
     InputStream in = new FileInputStream
( "InputToOutputStream.java" );
     OutputStream out = System.out;
     int nextChar;
     while ( ( nextChar = in.read() ) != -1  )
     out.write(  Character.toUpperCase( 
(char) nextChar ) );
     out.write( '\n' );
     out.flush();
     }
}

The output of the program is given below:
C:\rajesh\io>javac InputToOutputStream.java

C:\rajesh\io>java InputToOutputStream
IMPORT JAVA.IO.*;
PUBLIC CLASS INPUTTOOUTPUTSTREAM
{
PUBLIC STATIC VOID MAIN( STRING ARGS[] )THROWS IOEXCEPTION
{
INPUTSTREAM IN = NEW FILEINPUTSTREAM( "INPUTTOOUTPUTSTREAM.JAVA" );

OUTPUTSTREAM OUT = SYSTEM.OUT;

INT NEXTCHAR;

WHILE ( ( NEXTCHAR = IN.READ() ) != -1 )
OUT.WRITE( CHARACTER.TOUPPERCASE( (CHAR) NEXTCHAR ) );

OUT.WRITE( '\N' );
OUT.FLUSH();
}}
Download this example.

Website Design by Mayuri Multimedia