Unzip a folder and select a particular folder and again zip it

Mohit Saini :

I have one zipped folder(ex test.zip) which has following structure when I unzip it-

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

Now I have to remove WebContent from this structure. My final Unzip file should have config, css,js and index.html at root location.

Please let me know any efficient JAVA code to fist unzip test.zip then bypass the WebContent folder and again zip all the files present in WebContent.

Thanks in Advance!

Thomas Bitonti :

This is a common pattern / activity, so a good one to have in your tool kit.

See the code, below. There is a 'select' method, which is for you to customize as needed to drive selection of entries which are or are not to be transferred:

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

Note that a directory may have an entry for the directory itself (for example, "meta-inf/", as well as entries for files within the directory (for example, 'meta-inf/manifest.mf'). Skipping the directory entry will not skip the entries within the directory.

Note also, this avoids any unzipping to disk. That is to be avoided, since it adds a lot to the execution time. (But note also that entire entries are not loaded in memory; only a single bufferfull of data is ever in memory.)

Assuming the code is in 'transfer.jar', usage is:

Usage: sample.transfer.TransferSample inputPath outputPath

For example, running the tool on it's own jar:

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

Here is the code:

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;
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=400469&siteId=1