AIM:
To perform text compression iv Java.
ALGORITHM:
1. Start the program
2. Create 2 files as input and output and enter text in input file.
3. Calculate length of input file and compress it.
4. If f<0 br="" compression="" fails.="">
5. Else displays the compressed text in output file.
6. Stop the program.0>
SOURCE CODE:
import java.io.*;
import java.util.zip.*;
class compress
{
public static void main(String args[]) throws Exception
{
try
{
String a,b;
System.out.println("\nEnter the input file name:");
InputStreamReader cread=new InputStreamReader(System.in);
BufferedReader cin=new BufferedReader(cread);
a=cin.readLine();
System.out.println("\nEnter the output file:");
b=cin.readLine();
FileInputStream fis=new FileInputStream(a);
FileOutputStream fos=new FileOutputStream(b);
GZIPOutputStream gzos=new GZIPOutputStream(fos);
int temp=fis.read();
while(temp!=-1)
{
gzos.write(temp);
temp=fis.read();
}
gzos.close();
fos.close();
File f1=new File(a);
File f2=new File(b);
long f11=f1.length();
long f21=f2.length();
long f=f21-f11;
if(f<0 br="">
{
System.out.println("Compression Failed");
System.out.println("The file has gained "+f+"bytes");
}
else
{
System.out.println(" ");
System.out.println("File has been compressed");
System.out.println("Original size is :"+f21+"bytes");
System.out.println("Compressed size is :"+f11+"bytes");
long ds;
ds=(f21-f11);
System.out.println("Difference in size is :"+ds+"bytes");
float cr;
cr=(((f21-f11)*100)/f21/1f);
System.out.println("Compression ratio is "+cr+"%");
}
}
catch(Exception e)
{
System.out.println("Exception"+e);
}
}}0>
import java.io.*;
import java.util.zip.*;
class compress
{
public static void main(String args[]) throws Exception
{
try
{
String a,b;
System.out.println("\nEnter the input file name:");
InputStreamReader cread=new InputStreamReader(System.in);
BufferedReader cin=new BufferedReader(cread);
a=cin.readLine();
System.out.println("\nEnter the output file:");
b=cin.readLine();
FileInputStream fis=new FileInputStream(a);
FileOutputStream fos=new FileOutputStream(b);
GZIPOutputStream gzos=new GZIPOutputStream(fos);
int temp=fis.read();
while(temp!=-1)
{
gzos.write(temp);
temp=fis.read();
}
gzos.close();
fos.close();
File f1=new File(a);
File f2=new File(b);
long f11=f1.length();
long f21=f2.length();
long f=f21-f11;
if(f<0 br="">
{
System.out.println("Compression Failed");
System.out.println("The file has gained "+f+"bytes");
}
else
{
System.out.println(" ");
System.out.println("File has been compressed");
System.out.println("Original size is :"+f21+"bytes");
System.out.println("Compressed size is :"+f11+"bytes");
long ds;
ds=(f21-f11);
System.out.println("Difference in size is :"+ds+"bytes");
float cr;
cr=(((f21-f11)*100)/f21/1f);
System.out.println("Compression ratio is "+cr+"%");
}
}
catch(Exception e)
{
System.out.println("Exception"+e);
}
}}0>
No comments:
Post a Comment