Search in sources :

Example 11 with ZipEntry

use of org.apache.tools.zip.ZipEntry in project hive by apache.

the class CompressionUtils method zip.

public static void zip(String parentDir, String[] inputFiles, String outputFile) throws IOException {
    ZipOutputStream output = null;
    try {
        output = new ZipOutputStream(new FileOutputStream(new File(parentDir, outputFile)));
        for (int i = 0; i < inputFiles.length; i++) {
            File f = new File(parentDir, inputFiles[i]);
            FileInputStream input = new FileInputStream(f);
            output.putNextEntry(new ZipEntry(inputFiles[i]));
            try {
                IOUtils.copy(input, output);
            } finally {
                input.close();
            }
        }
    } finally {
        org.apache.hadoop.io.IOUtils.closeStream(output);
    }
}
Also used : ZipOutputStream(org.apache.tools.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(org.apache.tools.zip.ZipEntry) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 12 with ZipEntry

use of org.apache.tools.zip.ZipEntry in project wechat by dllwh.

the class ZipUtil method unZipFiles.

/**
 * @Title: unZipFiles
 * @Description: 解压文件到指定目录
 * @param zipFile
 * @param targetPath
 *            解压缩到的位置,如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下
 * @throws Exception
 */
public static void unZipFiles(File zipFile, String targetPath) {
    OutputStream os = null;
    InputStream is = null;
    ZipFile zip = null;
    if (StringUtilHelper.isBlank(targetPath)) {
        String zipPath = zipFile.getAbsolutePath();
        targetPath = zipPath.substring(0, zipPath.lastIndexOf("."));
    }
    File pathFile = new File(targetPath);
    if (!pathFile.exists()) {
        pathFile.mkdirs();
    }
    try {
        zip = new ZipFile(zipFile);
        for (Enumeration<?> entries = zip.getEntries(); entries.hasMoreElements(); ) {
            ZipEntry zipEntry = (ZipEntry) entries.nextElement();
            String zipEntryName = zipEntry.getName();
            InputStream in = zip.getInputStream(zipEntry);
            String outPath = FilenameUtils.concat(targetPath, zipEntryName).replaceAll("\\*", "/");
            outPath = new String(outPath.getBytes("utf-8"), "ISO8859-1");
            // 判断路径是否存在,不存在则创建文件路径
            File file = null;
            if (outPath.indexOf("/") > 0) {
                file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
            } else {
                file = new File(outPath.substring(0, outPath.lastIndexOf('\\')));
            }
            if (!file.exists()) {
                file.mkdirs();
            }
            // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
            if (new File(outPath).isDirectory()) {
                continue;
            }
            OutputStream out = new FileOutputStream(outPath);
            byte[] buf1 = new byte[BUFFEREDSIZE];
            int len;
            while ((len = in.read(buf1)) > 0) {
                out.write(buf1, 0, len);
            }
            in.close();
            out.close();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (zip != null) {
                zip.close();
            }
            if (os != null) {
                os.close();
            }
            if (is != null) {
                is.close();
            }
        } catch (Exception e) {
        }
    }
}
Also used : ZipFile(org.apache.tools.zip.ZipFile) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(org.apache.tools.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File)

Example 13 with ZipEntry

use of org.apache.tools.zip.ZipEntry in project gradle by gradle.

the class ZipFileTree method entriesSortedByName.

private Iterator<ZipEntry> entriesSortedByName(ZipFile zip) {
    Map<String, ZipEntry> entriesByName = new TreeMap<>();
    Enumeration<ZipEntry> entries = zip.getEntries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        entriesByName.put(entry.getName(), entry);
    }
    return entriesByName.values().iterator();
}
Also used : ZipEntry(org.apache.tools.zip.ZipEntry) TreeMap(java.util.TreeMap)

Example 14 with ZipEntry

use of org.apache.tools.zip.ZipEntry in project gradle by gradle.

the class ZipFileTree method visit.

@Override
public void visit(FileVisitor visitor) {
    File zipFile = fileProvider.get();
    if (!zipFile.exists()) {
        throw new InvalidUserDataException(format("Cannot expand %s as it does not exist.", getDisplayName()));
    }
    if (!zipFile.isFile()) {
        throw new InvalidUserDataException(format("Cannot expand %s as it is not a file.", getDisplayName()));
    }
    AtomicBoolean stopFlag = new AtomicBoolean();
    File expandedDir = getExpandedDir();
    try (ZipFile zip = new ZipFile(zipFile)) {
        // The iteration order of zip.getEntries() is based on the hash of the zip entry. This isn't much use
        // to us. So, collect the entries in a map and iterate over them in alphabetical order.
        Iterator<ZipEntry> sortedEntries = entriesSortedByName(zip);
        while (!stopFlag.get() && sortedEntries.hasNext()) {
            ZipEntry entry = sortedEntries.next();
            DetailsImpl details = new DetailsImpl(zipFile, expandedDir, entry, zip, stopFlag, chmod);
            if (entry.isDirectory()) {
                visitor.visitDir(details);
            } else {
                visitor.visitFile(details);
            }
        }
    } catch (Exception e) {
        throw new GradleException(format("Could not expand %s.", getDisplayName()), e);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ZipFile(org.apache.tools.zip.ZipFile) InvalidUserDataException(org.gradle.api.InvalidUserDataException) ZipEntry(org.apache.tools.zip.ZipEntry) GradleException(org.gradle.api.GradleException) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) GradleException(org.gradle.api.GradleException) InvalidUserDataException(org.gradle.api.InvalidUserDataException)

Example 15 with ZipEntry

use of org.apache.tools.zip.ZipEntry in project translationstudio8 by heartsome.

the class ZipFileExporter2 method addDbFolder.

public void addDbFolder(String destinationPath) throws IOException {
    ZipEntry newEntry = new ZipEntry(destinationPath);
    outputStream.putNextEntry(newEntry);
}
Also used : ZipEntry(org.apache.tools.zip.ZipEntry)

Aggregations

ZipEntry (org.apache.tools.zip.ZipEntry)32 ZipFile (org.apache.tools.zip.ZipFile)17 File (java.io.File)13 FileOutputStream (java.io.FileOutputStream)9 IOException (java.io.IOException)9 FileInputStream (java.io.FileInputStream)7 BufferedInputStream (java.io.BufferedInputStream)6 InputStream (java.io.InputStream)6 BufferedOutputStream (java.io.BufferedOutputStream)5 Enumeration (java.util.Enumeration)5 ZipException (java.util.zip.ZipException)4 BuildException (org.apache.tools.ant.BuildException)4 ZipOutputStream (org.apache.tools.zip.ZipOutputStream)4 OutputStream (java.io.OutputStream)3 ZipResource (org.apache.tools.ant.types.resources.ZipResource)3 FileNotFoundException (java.io.FileNotFoundException)2 RandomAccessFile (java.io.RandomAccessFile)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Date (java.util.Date)2 ZipOutputStream (java.util.zip.ZipOutputStream)2