Java JarOutputStream Write saveZip(InputStream is, String filename, JarOutputStream out)

Here you can find the source of saveZip(InputStream is, String filename, JarOutputStream out)

Description

save Zip

License

Open Source License

Declaration

public static void saveZip(InputStream is, String filename, JarOutputStream out) throws IOException 

Method Source Code


//package com.java2s;
import java.io.ByteArrayInputStream;

import java.io.IOException;
import java.io.InputStream;

import java.util.jar.JarOutputStream;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;

public class Main {
    public static void saveZip(InputStream is, String filename, JarOutputStream out) throws IOException {
        saveZip(is, filename, out, null);
    }/*from  ww  w.  j  a  v a 2  s.  c o  m*/

    public static void saveZip(String fileContent, String filename, JarOutputStream out, String trimPath)
            throws IOException {
        ByteArrayInputStream bis = new ByteArrayInputStream(fileContent.getBytes());
        saveZip(bis, filename, out, trimPath);

    }

    public static void saveZip(InputStream is, String filename, JarOutputStream out, String trimPath)
            throws IOException {
        byte[] buffer = new byte[4096];
        int len = -1;

        ZipEntry entry = new ZipEntry(convertPath(filename, trimPath));
        out.putNextEntry(entry);

        CRC32 crc32 = new CRC32();
        while ((len = is.read(buffer)) != -1) {
            out.write(buffer, 0, len);
            crc32.update(buffer, 0, len);
        }
        entry.setCrc(crc32.getValue());
        out.closeEntry();
    }

    public static String convertPath(String oriPath, String trimPath) {
        if (trimPath == null || trimPath.equals("")) {
            return oriPath;
        }
        oriPath.replace('\\', '/');
        trimPath.replace('\\', '/');

        if (oriPath.startsWith("/")) {
            oriPath = oriPath.substring(1);
        }
        if (trimPath.startsWith("/")) {
            trimPath = trimPath.substring(1);
        }
        int p = oriPath.indexOf(trimPath);
        if (p == 0) {
            oriPath = oriPath.substring(trimPath.length());
        }
        if (oriPath.startsWith("/")) {
            oriPath = oriPath.substring(1);
        }
        return oriPath;

    }
}