Android 压缩文件

来源:互联网 发布:mac 虚拟机 win10 编辑:程序博客网 时间:2024/05/16 12:32

不说话,看代码吧,还是说话吧….就是个简单的文件压缩,不如不说了…

  /**     * 打包压缩文件     *     * @return     */    private boolean ZipFileLog() {        SLog.Console("打包压缩文件");        boolean rtn = false;        try {            File zipFile = new File(dirPath, LogFileZip);            Collection<File> resFileList = new ArrayList<File>();            File f = new File(dirPath);            List<File> fileList = getFile(f);            for (int i = 0; i < fileList.size(); i++) {                File file = fileList.get(i);                if (file.getName().endsWith(".txt")) {                    resFileList.add(new File(dirPath, file.getName()));                }                Log.i("文件名字", file.getName());            }//            resFileList.add(new File(dirPath, DBFile));            zipFiles(resFileList, zipFile);            rtn = true;        } catch (Exception e) {            e.printStackTrace();        }        return rtn;    }    public List<File> getFile(File file) {        mFileList.clear();        File[] fileArray = file.listFiles();        for (File f : fileArray) {            if (f.isFile()) {                mFileList.add(f);            } else {                getFile(f);            }        }        return mFileList;    }    /**     * Gzip文件压缩     *     * @param resFileList     * @param zipFile     压缩文件     * @throws IOException IO错误     */    public static void zipFiles(Collection<File> resFileList, File zipFile)            throws IOException {        ZipOutputStream zipout = null;        try {            zipout = new ZipOutputStream(new BufferedOutputStream(                    new FileOutputStream(zipFile), BUFF_SIZE));            for (File resFile : resFileList) {                zipFile(resFile, zipout, "");            }        } finally {            if (zipout != null)                zipout.close();        }    }    /**     * 文件压缩     *     * @param resFile     * @param zipout     * @param rootpath     * @throws FileNotFoundException     * @throws IOException     */    private static void zipFile(File resFile, ZipOutputStream zipout,                                String rootpath) throws FileNotFoundException, IOException {        rootpath = rootpath                + (rootpath.trim().length() == 0 ? "" : File.separator)                + resFile.getName();        rootpath = new String(rootpath.getBytes("8859_1"), HTTP.UTF_8);        BufferedInputStream in = null;        try {            if (resFile.isDirectory()) {                File[] fileList = resFile.listFiles();                for (File file : fileList) {                    zipFile(file, zipout, rootpath);                }            } else {                byte buffer[] = new byte[BUFF_SIZE];                in = new BufferedInputStream(new FileInputStream(resFile),                        BUFF_SIZE);                zipout.putNextEntry(new ZipEntry(rootpath));                int realLength;                while ((realLength = in.read(buffer)) != -1) {                    zipout.write(buffer, 0, realLength);                }                in.close();                // 1.6报错//                zipout.flush();                zipout.closeEntry();            }        } finally {            if (in != null)                in.close();            // if (zipout != null);            // zipout.close();        }    }

解释下flush()

flush() 是清空,而不是刷新啊。

一般主要用在IO中,即清空缓冲区数据,就是说你用读写流的时候,其实数据是先被读到了内存中,然后用数据写到文件中,当你数据读完的时候不代表你的数据已经写完了,因为还有一部分有可能会留在内存这个缓冲区中。这时候如果你调用了 close()方法关闭了读写流,那么这部分数据就会丢失,所以应该在关闭读写流之前先flush(),先清空数据。

原创粉丝点击