JAVA压缩文件、解压文件、删除文件

主要要用到zip的文件输入输出流、对文件是目录还是非目录的判断,以及对文件的遍历

压缩

    public File zip(String filePath) {
        File target = null;
        File source = new File(filePath);
        if (source.exists()) {
            // 压缩文件名=源文件名.zip
            //这里可以对目标文件路径进行设置
            String zipName = source.getName() + ".zip";
            target = new File(source.getParent(), zipName);
            if (target.exists()) {
                target.delete(); // 删除旧的文件
            }
            FileOutputStream fos = null;
            ZipOutputStream zos = null;
            try {
                fos = new FileOutputStream(target);
                zos = new ZipOutputStream(new BufferedOutputStream(fos));
                // 添加对应的文件Entry
                File[] files = source.listFiles();
                for(int i = 0 ; i < files.length ;i++){
                    addEntry("", files[i], zos);
                }
                zos.close();
                fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            } 
        }
        return target;
    }

    private static void addEntry(String base, File source, ZipOutputStream zos)
            throws IOException {
        // 按目录分级,形如:/aaa/bbb.txt
        String entry = base + source.getName();
        if (source.isDirectory()) {
            for (File file : source.listFiles()) {
                // 递归列出目录下的所有文件,添加文件Entry
                addEntry(entry + "/", file, zos);
            }
        } else {
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                byte[] buffer = new byte[1024 * 10];
                fis = new FileInputStream(source);
                bis = new BufferedInputStream(fis, buffer.length);
                int read = 0;
                zos.putNextEntry(new ZipEntry(entry));
                while ((read = bis.read(buffer, 0, buffer.length)) != -1) {
                    zos.write(buffer, 0, read);
                }
                zos.closeEntry();
            } finally {
                bis.close();
                fis.close();
            }
        }
    }

解压

public void UnZip(String zipFileName , String armFilePath){   
//解压文件,zipFileName为压缩文件路径,armFilePath为解压文件夹路径
        try {
            ZipInputStream zis = new ZipInputStream(new FileInputStream(new File(zipFileName)));
            BufferedInputStream bis = new BufferedInputStream(zis);
            ZipEntry zipEntry;

            while((zipEntry = zis.getNextEntry()) != null && !zipEntry.isDirectory()){
                File nowFileOut = new File(armFilePath,zipEntry.getName());
                if(!nowFileOut.exists()){
                    (new File(nowFileOut.getParent())).mkdirs();
                }
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(nowFileOut));
                byte[] buffer = new byte[1024 * 10];
                int read = 0;
                while((read = bis.read(buffer,0,buffer.length)) != -1){
                    bos.write(buffer,0,read);
                }
                bos.close();
            }
            bis.close();
            zis.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

删除整个文件夹

    private boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i=0; iboolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空,可以删除
        return dir.delete();
    }

你可能感兴趣的:(android学习)