compressing a file

Hi.

I'm trying to compress a file, but no book I've got says anything about it, and I couldn't see much in the sun tutorials, etc.

Looking through the API, it seems like I should be using stuff in java.util.zip, but I don't know ehat to use, eg what is the best Zip format.

I tried this code:

FileInputStream fis = new FileInputStream( theFile );fis.read( b );

ZipOutputStream zout = new ZipOutputStream( new FileOutputStream( "newFile.zip" ) );

zout.write( b, 0, b.length );

but when I run it I get an IOException from the 'zout.write()' line. I'm guessing I'm just not using the right streams / using them in the right way.

Does anyone know anything about this, or where I can find out?

Thanks.

[794 byte] By [ebis] at [2007-9-26 8:01:42]
# 1

Hi,

Here is an example that archives all the log files out of a specific diretory.

package com.lendersservice.arc ;

import java.util.zip.* ;

import java.io.* ;

import java.util.Date ;

import java.text.SimpleDateFormat ;

public class ArchiveLogs extends Object {

public ArchiveLogs() { }

public static void main( String args[] ) {FilenameFilter select = new FileListFilter( "", "log" );String directory = "E:\\orant\\ows\\admin\\website40\\log\\" ;

ZipOutputStream out = null ;

try{

File myDir = new File( directory ) ;

String[] contents = myDir.list( select );System.out.println( "Processing files from " + myDir.getName() );

if ( contents != null ){

//create the zip file

SimpleDateFormat df = new SimpleDateFormat( "MMddyyyy" ) ;

Date today = new Date();

String outFilename = "logsfor" + df.format( today ) + ".zip";

out = new ZipOutputStream(new FileOutputStream( outFilename ));

for ( int i = 0 ; i < contents.length ; i++ ) { System.out.println(contents[ i ]); processFile(myDir, contents[ i ], out );

}

}

out.close();

}catch ( Exception e )

{System.out.println( "Error ::" + e );}

} static void processFile( File myDir , String fileName , ZipOutputStream out )

{try

{

File f = new File( myDir, fileName );

FileInputStream in = new FileInputStream( f );

// Add ZIP entry to output stream.

out.putNextEntry(new ZipEntry( fileName ));

byte[] buf = new byte[1024];

int len ;

while ( ( len = in.read( buf ) ) > 0) {

out.write( buf , 0 , len );}

out.closeEntry();

in.close();

f.delete();

}catch ( Exception e )

{System.out.println( "Error ::" + e );}

}

}

package com.lendersservice.arc ;

import java.io.* ;

import java.util.Date ;

public class FileListFilter implements FilenameFilter{private String name ;

private String extention ;

//constrictor public FileListFilter( String name , String extention ) {

this.name = name.toLowerCase();

this.extention = extention.toLowerCase(); }

public boolean accept( File directory , String filename ) {boolean fileOK = true ;

filename = filename.toLowerCase();

//If there is a name filter specified, check the file nameif ( name.length() != 0 )

fileOK &= filename.startsWith( name );

//If there is an extention specified check the the extention

if ( extention != null )

fileOK &= filename.endsWith( '.' + extention );return fileOK ; }

}

Hope this will help you.

Anil.

Developer Technical Support

Sun Microsystems Inc,

http://www.sun.com/developers/support

ramanil_indts at 2007-7-1 18:21:01 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
Hi ramanil_indts,thanx for the sample example provided. i will go through it .
ebis at 2007-7-1 18:21:01 > top of Java-index,Java HotSpot Virtual Machine,Specifications...