Java 압축 zip 형식 파일

7693 단어 javazip 압축
세 가지 주요 클래스:
  • import java.util.zip.ZipFile;//zip 형식 파일 열기..
  • import java.util.zip.ZipEntry;//압축된 파일의 속성을 표시합니다. 주로 파일 이름과 경로입니다..
  • import java.util.zip.ZipOutputStream;//zip 형식 출력 흐름..

  • 폴더를 압축할 때, 폴더의 모든 파일을 압축하는 데 한 개의 결점이 필요하다.
        public static void zipFile(String filePath, String zipPath) {
            try {
                File inFile = new File(filePath);
                File outFile = new File(zipPath);
                FileOutputStream fos = new FileOutputStream(outFile);
                ZipOutputStream zos = new ZipOutputStream(fos);
                zipFile(filePath, inFile, zos);
                zos.close();
                fos.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        /** *   * @author flueky [email protected] * @date 2016 4 13   7:47:11 * @param folderPath * @param zipPath */
        public static void zipFolder(String folderPath, String zipPath) {
    
            try {
                File inFile = new File(folderPath);
                File outFile = new File(zipPath);
                FileOutputStream fos = new FileOutputStream(outFile);
                ZipOutputStream zos = new ZipOutputStream(fos);
                zipFile(folderPath, inFile, zos);
                zos.close();
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        /** *   * @author flueky [email protected] * @date 2016 4 13   7:47:28 * @param folderPath * @param file * @param zos */
        private static void zipFile(String folderPath, File file, ZipOutputStream zos) {
    
            int index = folderPath.lastIndexOf(File.separator);
    
            try {
                if (file.isDirectory()) {//  , 
                    File[] files = file.listFiles();
                    if (files != null) {
                        for (File f : files)
                            zipFile(folderPath, f, zos);
                    }
                    return;
                }
                zos.putNextEntry(new ZipEntry(file.getAbsolutePath().substring(index)));
                byte[] buffer = new byte[1024];
                int length = -1;
                FileInputStream fis = new FileInputStream(file);
                while ((length = fis.read(buffer)) != -1) {
                    zos.write(buffer, 0, length);
                }
                fis.close();
    
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    
        /** *  .zip  * * @author flueky [email protected] * @date 2016 4 13   3:12:23 * @param zipFilePath * @param outFolderPath */
        public static void unzipFile(String zipFilePath, String outFolderPath) {
    
            try {
                ZipFile zipFile = new ZipFile(zipFilePath);
    
                Enumeration<ZipEntry> zList = (Enumeration<ZipEntry>) zipFile.entries();
                while (zList.hasMoreElements()) {
                    ZipEntry zipEntry = zList.nextElement();
                    if (zipEntry.getName().startsWith("__MACOSX"))// Mac
                                                                    //  , 
                        continue;
                    if (!zipEntry.isDirectory()) {//  
                        InputStream is = zipFile.getInputStream(zipEntry);
                        StringBuffer name = new StringBuffer(outFolderPath);
                        if (name.lastIndexOf(File.separator) != name.length() - 1) {
                            name.append(File.separator);
                        }
                        name.append(zipEntry.getName());
                        File outFile = new File(name.toString());
                        if (!outFile.getParentFile().exists())
                            outFile.getParentFile().mkdirs();
                        outFile.createNewFile();
                        FileOutputStream fos = new FileOutputStream(outFile);
                        int length = -1;
                        byte[] buffer = new byte[1024];
                        while ((length = is.read(buffer)) != -1) {
                            fos.write(buffer, 0, length);
                        }
                        //  
                        is.close();
                        fos.close();
                    }
    
                }
                zipFile.close();
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }

    좋은 웹페이지 즐겨찾기