Search in sources :

Example 1 with ZipEntry

use of org.apache.tools.zip.ZipEntry in project libresonic by Libresonic.

the class UploadController method unzip.

private void unzip(File file, List<File> unzippedFiles) throws Exception {
    LOG.info("Unzipping " + file);
    ZipFile zipFile = new ZipFile(file);
    try {
        Enumeration<?> entries = zipFile.getEntries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            File entryFile = new File(file.getParentFile(), entry.getName());
            if (!entry.isDirectory()) {
                if (!securityService.isUploadAllowed(entryFile)) {
                    throw new Exception("Permission denied: " + StringUtil.toHtml(entryFile.getPath()));
                }
                entryFile.getParentFile().mkdirs();
                InputStream inputStream = null;
                OutputStream outputStream = null;
                try {
                    inputStream = zipFile.getInputStream(entry);
                    outputStream = new FileOutputStream(entryFile);
                    byte[] buf = new byte[8192];
                    while (true) {
                        int n = inputStream.read(buf);
                        if (n == -1) {
                            break;
                        }
                        outputStream.write(buf, 0, n);
                    }
                    LOG.info("Unzipped " + entryFile);
                    unzippedFiles.add(entryFile);
                } finally {
                    IOUtils.closeQuietly(inputStream);
                    IOUtils.closeQuietly(outputStream);
                }
            }
        }
        zipFile.close();
        file.delete();
    } finally {
        zipFile.close();
    }
}
Also used : ZipFile(org.apache.tools.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(org.apache.tools.zip.ZipEntry) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File)

Example 2 with ZipEntry

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

the class ZipFileExporter2 method write.

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

Example 3 with ZipEntry

use of org.apache.tools.zip.ZipEntry in project tmdm-studio-se by Talend.

the class MDMImportItemUtilTest method createZipFile.

private void createZipFile(File sourceFile, String targetZipFile) {
    try {
        FileOutputStream target = new FileOutputStream(targetZipFile);
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(target));
        int BUFFER_SIZE = 1024;
        byte[] buff = new byte[BUFFER_SIZE];
        FileInputStream fi = new FileInputStream(sourceFile);
        BufferedInputStream origin = new BufferedInputStream(fi);
        ZipEntry entry = new ZipEntry(sourceFile.getName());
        out.putNextEntry(entry);
        int count;
        while ((count = origin.read(buff)) != -1) {
            out.write(buff, 0, count);
        }
        origin.close();
        out.close();
    } catch (IOException e) {
        // 
        log.error(e.getMessage(), e);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ZipOutputStream(org.apache.tools.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(org.apache.tools.zip.ZipEntry) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream)

Example 4 with ZipEntry

use of org.apache.tools.zip.ZipEntry in project javautils by jiadongpo.

the class FileUtil method unZipRedirect.

/**
 * 传入压缩包路径,解压,取里边名称包含det的文件,放在zip同目录,然后把解压后的文件夹删了。
 * 这个不是工具方法。
 *
 * @param files
 * @return
 * @throws IOException
 * @throws FileNotFoundException
 * @throws ZipException
 */
public static void unZipRedirect(List<File> files) throws IOException, FileNotFoundException, ZipException {
    for (File file : files) {
        // 压缩文件路径
        String packagePath = file.getAbsolutePath();
        String outPath = packagePath.substring(0, packagePath.length() - 4);
        String characterSet = "GBK";
        try {
            BufferedInputStream bi;
            if (characterSet == null) {
                // 默认GBK
                characterSet = "GBK";
            }
            // 支持中文
            ZipFile zf = new ZipFile(packagePath, characterSet);
            Enumeration e = zf.getEntries();
            while (e.hasMoreElements()) {
                ZipEntry ze2 = (ZipEntry) e.nextElement();
                String entryName = ze2.getName();
                String path = outPath + "/" + entryName;
                if (ze2.isDirectory()) {
                    System.out.println("正在创建解压目录 - " + entryName);
                    File decompressDirFile = new File(path);
                    if (!decompressDirFile.exists()) {
                        decompressDirFile.mkdirs();
                    }
                } else {
                    System.out.println("正在创建解压文件 - " + entryName);
                    String fileDir = path.substring(0, path.lastIndexOf("/"));
                    File fileDirFile = new File(fileDir);
                    if (!fileDirFile.exists()) {
                        fileDirFile.mkdirs();
                    }
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outPath + "/" + entryName));
                    bi = new BufferedInputStream(zf.getInputStream(ze2));
                    byte[] readContent = new byte[1024];
                    int readCount = bi.read(readContent);
                    while (readCount != -1) {
                        bos.write(readContent, 0, readCount);
                        readCount = bi.read(readContent);
                    }
                    bos.close();
                }
            }
            zf.close();
            // 删除过滤所有 以__MACOSX,.DS_Store文件目录
            File outFile = new File(outPath);
            File[] inner1Files = outFile.listFiles();
            if (inner1Files.length > 0) {
                for (File inner1File : inner1Files) {
                    if (inner1File.getName().startsWith("__MACOSX") || inner1File.getName().startsWith(".DS_Store")) {
                        deleteDir(inner1File);
                    }
                    if (inner1File.isDirectory()) {
                        File[] inner2Files = inner1File.listFiles();
                        if (inner2Files.length > 0) {
                            for (File inner2File : inner2Files) {
                                if (inner2File.getName().startsWith("__MACOSX") || inner2File.getName().startsWith(".DS_Store")) {
                                    deleteDir(inner1File);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            if (e.getMessage().equals("archive is not a ZIP archive")) {
                System.out.println("zip压缩文件[" + file.getAbsolutePath() + "]异常,文件不能解压!" + SysUtil.lineSeparator() + Const.getStackTracker(e));
                continue;
            }
            System.out.println("zip压缩文件[" + file.getAbsolutePath() + "]解压过程中发生异常!" + SysUtil.lineSeparator() + Const.getStackTracker(e));
            continue;
        } finally {
            // deleteDir(new File(packagePath)); // 删除压缩包
            // 获取解压后的文件全路径
            List<File> filedirs = com.cenrise.mailcbc.Const.searchFile(new File(outPath), ".det.");
            for (File onefile : filedirs) {
                // 移动文件到zip同级目录
                copy(onefile, file.getParent() + java.io.File.separator + onefile.getName());
            }
            // 删除生成的目录
            com.cenrise.mailcbc.Const.delFolder(outPath);
        }
    }
}
Also used : Enumeration(java.util.Enumeration) ZipEntry(org.apache.tools.zip.ZipEntry) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ZipFile(org.apache.tools.zip.ZipFile) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 5 with ZipEntry

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

the class ZipScanner method fillMapsFromArchive.

/**
 * Fills the file and directory maps with resources read from the
 * archive.
 *
 * @param src the archive to scan.
 * @param encoding encoding used to encode file names inside the archive.
 * @param fileEntries Map (name to resource) of non-directory
 * resources found inside the archive.
 * @param matchFileEntries Map (name to resource) of non-directory
 * resources found inside the archive that matched all include
 * patterns and didn't match any exclude patterns.
 * @param dirEntries Map (name to resource) of directory
 * resources found inside the archive.
 * @param matchDirEntries Map (name to resource) of directory
 * resources found inside the archive that matched all include
 * patterns and didn't match any exclude patterns.
 */
@Override
protected void fillMapsFromArchive(Resource src, String encoding, Map<String, Resource> fileEntries, Map<String, Resource> matchFileEntries, Map<String, Resource> dirEntries, Map<String, Resource> matchDirEntries) {
    File srcFile = src.asOptional(FileProvider.class).map(FileProvider::getFile).orElseThrow(() -> new BuildException("Only file provider resources are supported"));
    try (ZipFile zf = new ZipFile(srcFile, encoding)) {
        Enumeration<ZipEntry> e = zf.getEntries();
        while (e.hasMoreElements()) {
            ZipEntry entry = e.nextElement();
            Resource r = new ZipResource(srcFile, encoding, entry);
            String name = entry.getName();
            if (entry.isDirectory()) {
                name = trimSeparator(name);
                dirEntries.put(name, r);
                if (match(name)) {
                    matchDirEntries.put(name, r);
                }
            } else {
                fileEntries.put(name, r);
                if (match(name)) {
                    matchFileEntries.put(name, r);
                }
            }
        }
    } catch (ZipException ex) {
        throw new BuildException("Problem reading " + srcFile, ex);
    } catch (IOException ex) {
        throw new BuildException("Problem opening " + srcFile, ex);
    }
}
Also used : ZipResource(org.apache.tools.ant.types.resources.ZipResource) ZipFile(org.apache.tools.zip.ZipFile) ZipEntry(org.apache.tools.zip.ZipEntry) ZipResource(org.apache.tools.ant.types.resources.ZipResource) ZipException(java.util.zip.ZipException) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File)

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