Aqui est� uma classe que tenho h� muito tempo, por favor se 
melhorarem ela (sei que faltam algumas coisas) enviem de volta.

[]'s

Claudio


/*-------------------------------------------------------------------------*/
import java.util.zip.*;
import java.util.*;
import java.io.*;

/**
 *  This is an utility class to work with zip files. <p>
 *
 *  Basically has static methods for compress, uncompress. </p>
 *  <p>
 *
 *  The default buffer size is 4096 bytes.</p> <p>
 *
 *  </p>
 *
 *@date       20/10/2000
 */
public class ZipHandler {

    /**
     *  The default buffer size is 4096 bytes.
     */
    public final static int BUFFER_SIZE = 4096;


    // private instances can instantiate this class.
    private ZipHandler() { }


    /**
     *  This will compress all files contained in directory passed in 
<code>completePath</code>
     *  parameter and the output is a zipped file specified by 
<code>destiny</code>
     *  parameter. <p>
     *
     *  Example:</p> <br>
     *  <p>
     *
     *  <code>File dir = new File ("/home/claudio/dir");</code><br>
     *  <code>File zippedFile = new File 
("/home/claudio/myZippedFile.zip");</code>
     *  <br>
     *  <code>ZipHandler.compress (dir, zippedFile);</code></p>
     *
     *@param  completePath  a directory complete path. All filed inside 
it will be
     *      compressed.
     *@param  destiny       a complete file name path. The zipped file name.
     */
    public static void compress(File completePath, File destiny) {
        FileOutputStream fos = null;
        ZipOutputStream zos = null;
        try {
            File[] f = completePath.listFiles();
            fos = new FileOutputStream(destiny);
            zos = new ZipOutputStream(fos);
            for (int i = 0; i < f.length; i++) {
                if (!f[i].isDirectory()) {
                    String fileName = f[i].getAbsolutePath();

                    FileInputStream fis = null;
                    BufferedInputStream bis = null;
                    try {
                        fis = new FileInputStream(fileName);
                        bis = new BufferedInputStream(fis);
   
                        ZipEntry ze = new ZipEntry(f[i].getName());
                        zos.putNextEntry(ze);
   
                        byte[] block = new byte[BUFFER_SIZE];
                        int bytes_read = 0;
                        while ((bytes_read = bis.read(block)) != -1) {
                            zos.write(block, 0, bytes_read);
                        }
                    } finally {
                        zos.closeEntry();
                        close(bis);
                        close(fis);
                    }
                   
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(zos);
            close(fos);
        }
    }


   
    /**
     *  This will uncompress the file specified by <code>zipFileName</code>
     *  parameter and extract the files in tmp directory. <p>
     *
     *  Example:</p> <br>
     *  <p>
     *
     *  <code>File zippedFile = new File 
("/home/claudio/myZippedFile.zip");</code>
     *  <br>
     *  <code>ZipHandler.uncompress (zippedFile);</code></p>
     *
     *@param  zipFileName  the zipped file.
     */
    public static void uncompress(File zipFileName) {
        String fileName = zipFileName.getName().substring(0, 
zipFileName.getName().lastIndexOf('.'));
        File outputDir = new File(fileName);
        outputDir.mkdirs();

        uncompress(zipFileName, outputDir);
    }
   
    /**
     *  This will uncompress the file specified by <code>zipFileName</code>
     *  parameter and extract the files in tmp directory. <p>
     *
     *  Example:</p> <br>
     *  <p>
     *
     *  <code>File zippedFile = new File 
("c:\\any\\directory\\myZippedFile.zip");</code>
     *  <br>
     *  <code>ZipHandler.uncompress (zippedFile);</code></p>
     *
     *@param  zipFileName  the zipped file.
     */
    public static void uncompress(File zipFileName, File outputDir) {
        ZipFile zf = null;
        try {
            zf = new ZipFile(zipFileName);
            Enumeration entries = zf.entries();
            while (entries.hasMoreElements()) {
                ZipEntry ze = (ZipEntry) entries.nextElement();

                // this will get ONLY the file name, without the path.
                String entryName = new File(ze.getName()).getName();
                InputStream in = null;
                BufferedInputStream bis = null;
                FileOutputStream fout = null;
                try {
                    in = zf.getInputStream(ze);
                    bis = new BufferedInputStream(in);
                    fout = new FileOutputStream(new File(outputDir, 
entryName));
   
                    int bytes_read = 0;
                    byte[] block = new byte[BUFFER_SIZE];
                    while ((bytes_read = bis.read(block)) != -1) {
                        fout.write(block, 0, bytes_read);
                    }
                } finally {
                    close(bis);
                    close(in);
                    close(fout);
                }
               
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                zf.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }

  private static void close(InputStream in) {
        try {
            in.close();
        } catch (IOException ioe) {
            Log.println("IOException while closing a InputStream >> " + 
ioe);
        }
    }


    private static void close(OutputStream out) {
        try {
            out.close();
        } catch (IOException ioe) {
            Log.println("IOException while closing a OutputStream >> " + 
ioe);
        }
    }
}



Vladimir wrote:

> Onde encontro essas classes?
>
>     ----- Original Message -----
>     *From:* Rodrigo Campos <mailto:[EMAIL PROTECTED]>
>     *To:* [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
>     *Sent:* Monday, June 10, 2002 7:57 AM
>     *Subject:* RE: [java-list] Fazer Arquivo Zip.
>
>     Tiago
>      
>     Com as classes :
>      
>     ZipOutputStream
>     ZipEntry
>     FileOutputStream
>      
>     voce consegue gerar um zip em java, no javadoc dessas classes
>     existe exemplos e � bem simples de usar.
>      
>
>         -----Original Message-----
>         *From:* Thiago Lutti [mailto:[EMAIL PROTECTED]]
>         *Sent:* Friday, June 07, 2002 10:41 AM
>         *To:* [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
>         *Subject:* [java-list] Fazer Arquivo Zip.
>
>          Ola, estou tentando compactar uma pasta inteira, alguem sabe
>         como fazer isso ??
>
>         Obrigado,
>
>         Thiago Ramalho Lutti
>         PadTec - Optical Components and Systems
>         Campinas / SP
>         e-mail : [EMAIL PROTECTED]
>         <mailto:[EMAIL PROTECTED]>http://www.padtec.com.br
>         <http://www.padtec.com.br/>
>



------------------------------ LISTA SOUJAVA ---------------------------- 
http://www.soujava.org.br  -  Sociedade de Usu�rios Java da Sucesu-SP 
d�vidas mais comuns: http://www.soujava.org.br/faq.htm
regras da lista: http://www.soujava.org.br/regras.htm
historico: http://www.mail-archive.com/java-list%40soujava.org.br
para sair da lista: envie email para [EMAIL PROTECTED] 
-------------------------------------------------------------------------

Responder a