Compactar Arquivos de diretorio

0 respostas
V

Ola, pessoal tenho q fazer o seguinte servico ex:
C:\Log\Log1\Log2, dentro desses diretorio tenho diversos arquivos .xlg, tenho que entrar dentro de cada diretorio e compactar os arquivos criando dentro de cada diretorio, vamos supor no diretorio Log compacto tods os arquivos e deixo ali dentro, dai no diretorio Log1 tbm compacto os arquivos mas salvo dentro desse mesmo diretorio, fiz um procedimento mas nao faz o que eu quero, pois ele compacta todos os arquivos e sub diretorio jogando em um unico arquivo compactado, mas tenho q para cada diretorio ter seu arquivo…

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.zip.Adler32;

import java.util.zip.CheckedOutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Teste {

/**
   *  um arquivo passado no parâmetro para a memória.
   * @param file Arquivo a ser lido.
   * @return Arquivo lido em bytes.
   * @throws Exception
   */
private byte[] read(File file) throws Exception {

byte[] result = null;

if (file != null && !file.isDirectory()) {

final long length = file.length();

result = new byte[(int) length];

InputStream fi = new FileInputStream(file);

byte b;

long count = 0;

while ((b = (byte) fi.read()) != -1) {

result[(int) count++] = b;

}

fi.close();

}

return result;

}

/**

  • Adiciona um diretório ou arquivo a um ZipOutputStream instanciado.

  • @param C:\teste.zip

  • @param C:\Documents and Settings\vanessa\workspace\Testando\teste.zip

  • @throws Exception
    
    */
    
    private void addToZip(ZipOutputStream out, File file, String path) throws Exception {
    
    byte data[] = null;
    
    ZipEntry entry = null;
    
    if (file != null) {
    
    String name = file.getAbsolutePath();
    
    name = name.substring(path.length() + 1, name.length()).replace( “\”,"/");
    
    System.out.println(">>>> Adding: " + name);
    
    if (file != null) {
    
    if (file.isDirectory()) {
    
    File[] files = file.listFiles();
    
    for (File f : files) {
    
    addToZip(out, f, path);
    
    }
    
    } else {
    
    entry = new ZipEntry(name);
    
    out.putNextEntry(entry);
    
    data = read(file);
    
    if (data != null && data.length > 0) {
    
    out.write(data, 0, data.length);
    
    }
    
    out.closeEntry();
    
    out.flush();
    
    }
    
    }
    
    }
    
    }
    

/**

  • Comprime um diretório ou arquivo.
  • @param zipName - Nome no arquivo zip que será gerado, exemplo: C:\Documents and Settings\vanessa\workspace\Testando\myzip.zip
  • @param dirName - Nome do arquivo ou diretório a ser comprimido.
    */
public void zip(String zipName, String dirName) {

ZipOutputStream out = null;

FileOutputStream dest = null;

CheckedOutputStream checksum = null;

try {

dest = new FileOutputStream(new File(zipName));

checksum = new CheckedOutputStream(dest, new Adler32());

out = new ZipOutputStream(new BufferedOutputStream(checksum));

File dir = new File(dirName);

String parent = dir.getParent();

int length = parent.length();

String substring = parent.substring(0, length);

addToZip(out, dir, substring);

System.out.println(">>>> checksum: " + checksum.getChecksum().getValue());

} catch (Exception e) {

e.printStackTrace();

} catch (Error err) {

err.printStackTrace();

} finally {

try {

out.flush();

out.finish();

out.close();

} catch (IOException e) {

e.printStackTrace();

} catch (Error err) {

err.printStackTrace();

}

}

}
public static void main(String[] args)

{

new Teste().zip(C:\log\compactados.zip, C:\compactados);

}

}
Criado 22 de maio de 2007
Respostas 0
Participantes 1