美文网首页
Android中简单使用Zip

Android中简单使用Zip

作者: chenTovae | 来源:发表于2016-08-13 02:11 被阅读181次

    初次使用Zip的压缩和解压,只是简单使用

    1、android中Zip的压缩:

    • Zip的压缩主要用到了ZipOutputStream和ZipEntry类
    • 小例子如下:
    public String zipFileOptions(){
        //创建压缩文件的路径
        String zipFileName = Environment.getExternalStorageDirectory() 
        + "/" + UUID.randomUUID().toString().replace("-", "") + ".zip";
        //创建压缩的文件对象
        File zipFile = new File(zipFileName);
        //创建InputStream对象
        InputStream is = null;
        //创建ZipOutputStream对象
        ZipOutputStream zos = null;
        try{
            //获取ZipOutputStream对象实例
            zos = new ZipOutputStream(new FileOutputStream(zipFile));
            zos.setComment("hello");
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }
        //FileBean中保存有需要压缩的文件的文件路径,mFileList中保存了所有需要被压缩的文件路径
        for(FileBean bean : mFileList){
            //根据路径,创建需要被压缩的文件对象,bean.getUrlPath()获取到的是文件的路径
            File file = new File(bean.getUrlPath());
            try{
                //获取输入流对象
                is = new FileInputStream(file);
                zos.setNextEntry(new ZipEntry(file.getName()));
                byte[] buffer = new byte[8*1024];
                int length = 0;
                while((length=is.read(buffer))!=-1){
                    //将文件写进压缩流
                    zos.write(buffer,0,length);
                }
                is.close(); 
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    
        try{
            zos.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return zipFileName;
    }
    

    注意:文件的压缩相对来说比较耗费时间,因此,不能在主线程中进行操作,需要另起线程进行操作,以上例子用到的是AsyncTask。

    2、Zip的下载:

    • 下载文件用到了java原始的API,在下载之前,首先需要获得被下载文件的URL。
    • 然后自定义一个本地的文件路径,用于装载下载的Zip文件。
    • 然后在异步任务中进行文件的下载。
    • 小例子如下:
    /**
    * 文件的下载方法
    * @param url 文件的URL
    * @param out 文件需要下载到的指定路径目录,需要自定义创建该路径
    **/
    public File downloadZipFile(String url,String out){
    
        //定义URL对象
        URL mURL = null;
        //定义File对象
        File mFile = null;
        //定义URLConnection对象
        URLConnection urlConnection = null;
        int byteCopies = 0;
        //定义FileOutputStream对象
        FileOutputStream mOutputFileStream = null;
    
        try{
            //创建URL实例
            mURL = new URL(url);
            //获取文件的名称
            String fileName = new File(mURL.getFile()).getName();
            //根据指定文件目录和文件名称来创建文件
            mFile = new File(out,fileName);
    
            //获取URLConnection对象
            urlConnection = mURL.openConnection();
            //获取文件的长度
            int length = urlConnection.getContentLength();
    
            //如果文件已经存在
            if (mFile.exists()&&length==mFile.length) {
                return mFile;
            }
    
            //获取FileOutputStream对象
            mOutputFileStream = new FileOutputStream(mFile);
            //获取InputStream对象
            InputStream is = urlConnection.getInputStream();
    
            //设置缓冲区的大小
            byte[] buffer = new byte[8*1024];
            //创建BufferedInputStream对象
            BufferedInputStream bis = new BufferedInputStream(is,8*1024);
            //创建BufferedOutputStream对象
            BufferedOutputStream bos = new BufferedOutputStream(mOutputFileStream,8*1024);
    
            int n = 0;
    
            while((n = bis.read(buffer,0,8*1024)) != -1){
                bos.write(buffer,0,n);
            }
            //清空缓冲区
            bos.flush();
    
            return mFile;
    
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                bos.close();
                bis.close();
    
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    
        return null;
    
    }
    

    Zip的解压:

    • 首先需要获取到下载到的压缩文件
    • 完了以后通过异步任务对文件进行压缩
    • 下面的小例子通过解压出来的文件名称,再加上指定的文件目录来保存解压出来的文件
    • 小例子:
    /**
    * 进行文件解压的方法
    * @param file 下载回来的压缩文件
    **/
    public List<File> unZipFile(File file){
    
        //创建集合,保存解压出来的文件
        List<File> mFileList = new ArrayList<>();
        //定义解压出来的文件对象
        File outFile = null;
    
        try{
            //创建ZipFile对象
            ZipFile mZipFile = new ZipFile(file);
            //创建ZipInputStream对象
            ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
            //创建ZipEntry对象
            ZipEntry zipEntry = null;
            //定义InputStream对象
            InputStream is = null;
            //定义OutputStream对象
            OutputStream os = null;
    
            while((zipEntry=zis.getNextEntry())!=null){
                //拼凑路径,创建解压出来的文件对象
                outFile = new File(Environment.getExternalStorageDirectory() 
                    + "/vaeh" + File.separator + zipEntry.getName());
    
                //判断父级目录是否存在
                if (!outFile.getParentFile().exists()) {
                    //创建父级目录
                    outFile.getParentFile().mkdir();
                }
    
                //判断文件是否存在
                if (!outFile.exists()) {
                    //创建文件
                    outFile.createNewFile();
                }
    
                //获取is的实例
                is = mZipFile.getInputStream(zipEntry);
                os = new FileOutputStream(outFile);
    
                //创建缓冲区
                byte[] buffer = new byte[8*1024];
                int length = 0;
    
                while((length=is.read(buffer))!=-1){
                    os.write(buffer,0,length);
                }
                //这里加多一次判断是为了保险起见,防止出现空指针
                if (outFile.exists()) {
                    //将文件保存到集合中
                    mFileList.add(outFile);
                }
                is.close();
                os.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return mFileList;
    }
    

    相关文章

      网友评论

          本文标题:Android中简单使用Zip

          本文链接:https://www.haomeiwen.com/subject/yymesttx.html