Descomprimir una carpeta y seleccione una carpeta en particular y de nuevo la cremallera

Mohit Saini:

Tengo una carpeta comprimida (ex test.zip) que ha siguiente estructura cuando la cremallera de IT

WebContent/config/somefiles
WebContent/css/somefile
Webcontent/fonts/somefiles
Webcontent/js/somefiles
WebContent/index.html

Ahora tengo que quitar WebContent de esta estructura. Mi archivo final Descomprimir debe tener config, css,jsy index.htmlen la ubicación de la raíz.

Por favor, hágamelo saber cualquier código JAVA eficiente para descomprimir el puño test.zip se desviarán de la carpeta WebContent y otra vez zip todos los archivos presentes en WebContent.

¡Gracias por adelantado!

Thomas Bitonti:

Este es un patrón / actividad común, por lo que una buena para tener en su caja de herramientas.

Ver el código, a continuación. Hay un método de 'seleccionar', que es para personalizar según sea necesario para la selección de unidades de entradas que estén o no puede ser transferido:

private static boolean select(String entryName) {
    return true; // Customize me
}

Tenga en cuenta que un directorio puede tener una entrada para el propio directorio (por ejemplo, "meta-inf /", así como entradas para los archivos dentro del directorio (por ejemplo, 'meta-inf / manifest.mf'). Saltarse el directorio entrada no se hará saltar las entradas en el directorio.

Tenga en cuenta también, esto evita cualquier descomprimir el disco. Es decir que debe evitarse, ya que agrega mucho a la hora de ejecución. (Pero tenga en cuenta también que las entradas enteras no se cargan en la memoria, y sólo una única bufferfull de datos está siempre en la memoria.)

Suponiendo que el código está en 'transfer.jar', el uso es:

Uso: sample.transfer.TransferSample InputPath OutputPath

Por ejemplo, utilizando la herramienta en su propio frasco:

java -classpath transfer.jar sample.transfer.TransferSample transfer.jar transfer.jar.out

Transfer from [ transfer.jar ]
Transfer to [ transfer.jar.out ]
Transfer from [ c:\dev\transfer\transfer.jar ] (absolute path)
Transfer to [ c:\dev\transfer\transfer.jar.out ] (absolute path)
Entry [ META-INF/MANIFEST.MF ]: Selected
Transferred [ 25 ] bytes
Entry [ sample/ ]: Selected
Entry [ sample/transfer/ ]: Selected
Entry [ sample/transfer/TransferSample.class ]: Selected
Transferred [ 4061 ] bytes
Entry [ sample/transfer/TransferSample.java ]: Selected
Transferred [ 3320 ] bytes
Transfer Success

Aquí está el código:

package sample.transfer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class TransferSample {

    public static void main(String args[]) {
        if ( args.length < 2 ) {
            System.out.println("Usage: " + TransferSample.class.getName() + " inputPath outputPath");
            return;
        }

        String inputPath = args[0];
        String outputPath = args[1];

        System.out.println("Transfer from [ " + inputPath + " ]");
        System.out.println("Transfer to [ " + outputPath + " ]");

        File inputFile = new File(inputPath);
        File outputFile = new File(outputPath);

        System.out.println("Transfer from [ " + inputFile.getAbsolutePath() + " ] (absolute path)");
        System.out.println("Transfer to [ " + outputFile.getAbsolutePath() + " ] (absolute path)");

        try ( InputStream inputStream = new FileInputStream(inputFile) ) { // throws IOException
            try ( OutputStream outputStream = new FileOutputStream(outputFile) ) { // throws IOException
                transfer(inputStream, outputStream);
                System.out.println("Transfer Success");
            }
        } catch ( IOException e ) {
            System.err.println("Transfer Failure");
            e.printStackTrace(System.err);
        }
    }

    // All static to keep the code simpler.

    public static void transfer(InputStream inputStream, OutputStream outputStream) throws IOException {
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
        try {
            transfer(zipInputStream, zipOutputStream); // throws IOException
        } finally {
            zipOutputStream.finish(); // throws IOException
        }
    }

    private static final int BUFFER_SIZE = 64 * 1024;

    private static boolean select(String entryName) {
        return true; // Customize me
    }

    public static void transfer(ZipInputStream zipInputStream, ZipOutputStream zipOutputStream) throws IOException {
        byte[] buffer = new byte[BUFFER_SIZE]; // Single buffer for all transfers

        ZipEntry inputEntry;
        while ( (inputEntry = zipInputStream.getNextEntry()) != null ) {
            String entryName = inputEntry.getName();

            if ( !select(entryName) ) {
                System.out.println("Entry [ " + entryName + " ]: Not selected");
                continue;
            } else {
                System.out.println("Entry [ " + entryName + " ]: Selected");
            }

            zipOutputStream.putNextEntry(inputEntry); // throws IOException

            if ( !inputEntry.isDirectory() ) {
                long bytesTransferred = transfer(zipInputStream, zipOutputStream, buffer); // throws IOException
                System.out.println("Transferred [ " + bytesTransferred + " ] bytes");

                zipOutputStream.closeEntry(); // throws IOException
            }
        }
    }

    private static long transfer(InputStream inputStream, OutputStream outputStream, byte[] buffer) throws IOException {
        long totalBytesRead = 0L;

        int bytesRead = 0;
        while ( (bytesRead = inputStream.read(buffer, 0, buffer.length)) != -1 ) { // throws IOEXception
            totalBytesRead += bytesRead;
            outputStream.write(buffer, 0, bytesRead);
        }

        return totalBytesRead;
    }
}

Supongo que te gusta

Origin http://10.200.1.11:23101/article/api/json?id=400919&siteId=1
Recomendado
Clasificación