Search in sources :

Example 21 with ZipEntry

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

the class YeePayFetch method unzip.

/**
 * 传入压缩包路径,已传入字符集解压文件到相应文件目录(zip)
 *
 * @param packagePath
 * @param outPath
 * @param characterSet
 * @return
 */
public static boolean unzip(String packagePath, String outPath, String characterSet) throws IOException, FileNotFoundException, ZipException {
    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);
                            }
                        }
                    }
                }
            }
        }
        return true;
    } catch (Exception e) {
        System.out.println("zip解压失败!characterSet[" + characterSet + "]");
        e.printStackTrace();
        return false;
    } finally {
        // 东航压缩包内直接是各地区压缩包,解决方法,如果解压后0.7都为压缩包则将压缩包放入新建的文件夹内,模拟国航的目录结构
        // 最外层压缩包内文件,国航为一个文件夹,通用为csv文件,东航为各地区压缩包
        File[] afterUpZipFile = new File(outPath).listFiles();
        int total = afterUpZipFile.length;
        int zipNum = 0;
        for (File file : afterUpZipFile) {
            if (file.getName().endsWith(".zip")) {
                zipNum++;
            }
        }
        if ((zipNum / total) > 0.7) {
            String newFilePath = outPath + File.separator + new File(packagePath).getName();
            File newFile = new File(newFilePath);
            newFile.mkdir();
            for (File file : afterUpZipFile) {
                file.renameTo(new File(newFile.getPath() + File.separator + file.getName()));
            }
        }
        // 删除压缩包
        deleteDir(new File(packagePath));
    }
}
Also used : ZipFile(org.apache.tools.zip.ZipFile) ZipEntry(org.apache.tools.zip.ZipEntry) ZipFile(org.apache.tools.zip.ZipFile) ZipException(java.util.zip.ZipException)

Example 22 with ZipEntry

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

the class FileUtil method unzip.

/**
 * 传入压缩包路径,已传入字符集解压文件到相应文件目录(zip)
 * 删了压缩文件
 * @param packagePath
 * @param outPath
 * @param characterSet
 * @return
 */
public static boolean unzip(String packagePath, String outPath, String characterSet) throws IOException, FileNotFoundException, ZipException {
    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);
                            }
                        }
                    }
                }
            }
        }
        return true;
    } catch (Exception e) {
        System.out.println("zip解压失败!characterSet[" + characterSet + "]");
        e.printStackTrace();
        return false;
    } finally {
        // 东航压缩包内直接是各地区压缩包,解决方法,如果解压后0.7都为压缩包则将压缩包放入新建的文件夹内,模拟国航的目录结构
        // 最外层压缩包内文件,国航为一个文件夹,通用为csv文件,东航为各地区压缩包
        File[] afterUpZipFile = new File(outPath).listFiles();
        int total = afterUpZipFile.length;
        int zipNum = 0;
        for (File file : afterUpZipFile) {
            if (file.getName().endsWith(".zip")) {
                zipNum++;
            }
        }
        if ((zipNum / total) > 0.7) {
            String newFilePath = outPath + File.separator + new File(packagePath).getName();
            File newFile = new File(newFilePath);
            newFile.mkdir();
            for (File file : afterUpZipFile) {
                file.renameTo(new File(newFile.getPath() + File.separator + file.getName()));
            }
        }
        // 删除压缩包
        deleteDir(new File(packagePath));
    }
}
Also used : Enumeration(java.util.Enumeration) ZipFile(org.apache.tools.zip.ZipFile) BufferedInputStream(java.io.BufferedInputStream) ZipEntry(org.apache.tools.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 23 with ZipEntry

use of org.apache.tools.zip.ZipEntry in project free-framework by a601942905git.

the class ZipUtil method zipFileOrDirectory.

/**
 * 递归压缩文件或目录
 *
 * @param out
 *            压缩输出流对象
 * @param fileOrDirectory
 *            要压缩的文件或目录对象
 * @param curPath
 *            当前压缩条目的路径,用于指定条目名称的前缀
 * @throws IOException
 */
private static void zipFileOrDirectory(ZipOutputStream out, File fileOrDirectory, String curPath) throws IOException {
    FileInputStream in = null;
    try {
        if (!fileOrDirectory.isDirectory()) {
            // 压缩文件
            byte[] buffer = new byte[4096];
            int bytes_read;
            in = new FileInputStream(fileOrDirectory);
            ZipEntry entry = new ZipEntry(curPath + fileOrDirectory.getName());
            out.putNextEntry(entry);
            while ((bytes_read = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytes_read);
            }
            out.closeEntry();
        } else {
            // 压缩目录
            File[] entries = fileOrDirectory.listFiles();
            for (int i = 0; i < entries.length; i++) {
                // 递归压缩,更新curPaths
                zipFileOrDirectory(out, entries[i], curPath + fileOrDirectory.getName() + "/");
            }
        }
    } catch (IOException ex) {
        ex.printStackTrace();
        throw ex;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ex) {
            }
        }
    }
}
Also used : ZipEntry(org.apache.tools.zip.ZipEntry) ZipFile(org.apache.tools.zip.ZipFile)

Example 24 with ZipEntry

use of org.apache.tools.zip.ZipEntry in project free-framework by a601942905git.

the class ZipUtil method unzip.

/**
 * 解压缩
 *
 * @param zipFileName
 *            源文件
 * @param outputDirectory
 *            解压缩后文件存放的目录
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public static void unzip(String zipFileName, String outputDirectory) throws IOException {
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(zipFileName);
        Enumeration e = zipFile.getEntries();
        ZipEntry zipEntry = null;
        File dest = new File(outputDirectory);
        dest.mkdirs();
        while (e.hasMoreElements()) {
            zipEntry = (ZipEntry) e.nextElement();
            String entryName = zipEntry.getName();
            InputStream in = null;
            FileOutputStream out = null;
            try {
                if (zipEntry.isDirectory()) {
                    String name = zipEntry.getName();
                    name = name.substring(0, name.length() - 1);
                    File f = new File(outputDirectory + File.separator + name);
                    f.mkdirs();
                } else {
                    int index = entryName.lastIndexOf("\\");
                    if (index != -1) {
                        File df = new File(outputDirectory + File.separator + entryName.substring(0, index));
                        df.mkdirs();
                    }
                    index = entryName.lastIndexOf("/");
                    if (index != -1) {
                        File df = new File(outputDirectory + File.separator + entryName.substring(0, index));
                        df.mkdirs();
                    }
                    File f = new File(outputDirectory + File.separator + zipEntry.getName());
                    // f.createNewFile();
                    in = zipFile.getInputStream(zipEntry);
                    out = new FileOutputStream(f);
                    int c;
                    byte[] by = new byte[1024];
                    while ((c = in.read(by)) != -1) {
                        out.write(by, 0, c);
                    }
                    out.flush();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
                throw new IOException("解压失败:" + ex.toString());
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException ex) {
                    }
                }
            }
        }
    } catch (IOException ex) {
        ex.printStackTrace();
        throw new IOException("解压失败:" + ex.toString());
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException ex) {
            }
        }
    }
}
Also used : Enumeration(java.util.Enumeration) ZipFile(org.apache.tools.zip.ZipFile) ZipEntry(org.apache.tools.zip.ZipEntry) ZipFile(org.apache.tools.zip.ZipFile)

Example 25 with ZipEntry

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

the class PermissionUtilsTest method getSetPermissionsWorksForZipResources.

@Test
public void getSetPermissionsWorksForZipResources() throws IOException {
    File f = File.createTempFile("ant", ".zip");
    f.deleteOnExit();
    try (ZipOutputStream os = new ZipOutputStream(f)) {
        ZipEntry e = new ZipEntry("foo");
        os.putNextEntry(e);
        os.closeEntry();
    }
    ZipResource r = new ZipResource();
    r.setName("foo");
    r.setArchive(f);
    Set<PosixFilePermission> s = EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_READ);
    PermissionUtils.setPermissions(r, s, null);
    assertEquals(s, PermissionUtils.getPermissions(r, null));
}
Also used : ZipResource(org.apache.tools.ant.types.resources.ZipResource) ZipOutputStream(org.apache.tools.zip.ZipOutputStream) ZipEntry(org.apache.tools.zip.ZipEntry) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) File(java.io.File) Test(org.junit.Test)

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