分享好友 移动开发首页 频道列表

android 解压 ZIP 文件 - kaixinbingju 的专栏 - 博客频道 - CSDN.NET

Android开发  2016-12-01 15:420

AndroidManifest.xml 里添加权限:

<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

<uses-permissionandroid:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

工具类:

public class ZIP {
	public ZIP(){
		
	}
	
	 /** 
     * DeCompress the ZIP to the path 
     * @param zipFileString  name of ZIP 
     * @param outPathString   path to be unZIP
     * @throws Exception 
     */  
    public static void UnZipFolder(String zipFileString, String outPathString) throws Exception {  
        ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));  
        ZipEntry zipEntry;  
        String szName = "";  
        while ((zipEntry = inZip.getNextEntry()) != null) {  
            szName = zipEntry.getName();  
            if (zipEntry.isDirectory()) {  
                // get the folder name of the widget  
                szName = szName.substring(0, szName.length() - 1);  
                File folder = new File(outPathString + File.separator + szName);  
                folder.mkdirs();  
            } else {  
          
                File file = new File(outPathString + File.separator + szName);  
                file.createNewFile();  
                // get the output stream of the file  
                FileOutputStream out = new FileOutputStream(file);  
                int len;  
                byte[] buffer = new byte[1024];  
                // read (len) bytes into buffer  
                while ((len = inZip.read(buffer)) != -1) {  
                    // write (len) byte from buffer at the position 0  
                    out.write(buffer, 0, len);  
                    out.flush();  
                }  
                out.close();  
            }  
        } 
        inZip.close();  
    }
      
    /** 
     * Compress file and folder
     * @param srcFileString   file or folder to be Compress
     * @param zipFileString   the path name of result ZIP
     * @throws Exception 
     */  
    public static void ZipFolder(String srcFileString, String zipFileString)throws Exception {  
        //create ZIP 
        ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString));  
        //create the file 
        File file = new File(srcFileString);  
        //compress
        ZipFiles(file.getParent()+File.separator, file.getName(), outZip);  
        //finish and close
        outZip.finish();  
        outZip.close();  
    }
    
    /** 
     * compress files
     * @param folderString 
     * @param fileString 
     * @param zipOutputSteam 
     * @throws Exception 
     */  
    private static void ZipFiles(String folderString, String fileString, ZipOutputStream zipOutputSteam)throws Exception{  
        if(zipOutputSteam == null)  
        return;  
        File file = new File(folderString+fileString);  
        if (file.isFile()) {  
            ZipEntry zipEntry =  new ZipEntry(fileString);  
            FileInputStream inputStream = new FileInputStream(file);  
            zipOutputSteam.putNextEntry(zipEntry);  
            int len;  
            byte[] buffer = new byte[4096];   
            while((len=inputStream.read(buffer)) != -1)  
            {  
                zipOutputSteam.write(buffer, 0, len);  
            }  
            zipOutputSteam.closeEntry();  
        }  
        else {  
            //folder
            String fileList[] = file.list();  
            //no child file and compress  
            if (fileList.length <= 0) {  
                ZipEntry zipEntry =  new ZipEntry(fileString+File.separator);  
                zipOutputSteam.putNextEntry(zipEntry);  
                zipOutputSteam.closeEntry();                  
            }  
            //child files and recursion  
            for (int i = 0; i < fileList.length; i++) {  
                ZipFiles(folderString, fileString+java.io.File.separator+fileList[i], zipOutputSteam);  
            }//end of for  
        }    
    }
	
    /** 
     * return the InputStream of file in the ZIP
     * @param zipFileString  name of ZIP 
     * @param fileString     name of file in the ZIP 
     * @return InputStream 
     * @throws Exception 
     */  
    public static InputStream UpZip(String zipFileString, String fileString)throws Exception {  
        ZipFile zipFile = new ZipFile(zipFileString);  
        ZipEntry zipEntry = zipFile.getEntry(fileString);  
        return zipFile.getInputStream(zipEntry);  
    }  
    
	/** 
     * return files list(file and folder) in the ZIP
     * @param zipFileString     ZIP name
     * @param bContainFolder    contain folder or not
     * @param bContainFile      contain file or not
     * @return 
     * @throws Exception 
     */  
    public static List<File> GetFileList(String zipFileString, boolean bContainFolder, boolean bContainFile)throws Exception {  
        List<File> fileList = new ArrayList<File>();  
        ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));  
        ZipEntry zipEntry;  
        String szName = "";  
        while ((zipEntry = inZip.getNextEntry()) != null) {  
            szName = zipEntry.getName();  
            if (zipEntry.isDirectory()) {  
                // get the folder name of the widget  
                szName = szName.substring(0, szName.length() - 1);  
                File folder = new File(szName);  
                if (bContainFolder) {  
                    fileList.add(folder);  
                }  
          
            } else {  
                File file = new File(szName);  
                if (bContainFile) {  
                    fileList.add(file);  
                }  
            }  
        }
        inZip.close();  
        return fileList;  
    }  
}

查看更多关于【Android开发】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
Supporting Multiple Screens
术语和概念Screen size 屏幕尺寸又称「屏幕大小」,是屏幕对角线的物理尺寸。单位英寸 inch,比如 Samsung Note4 是 5.7 英寸。Resolution 屏幕分辨率屏幕纵横方向上物理像素的总数,比如 Samsung Note4 是 2560x1440,表示纵向有 2560 个像素,横向有 1440

0评论2017-02-05363

Android插件化(4):OpenAtlasの插件的卸载与更新
如果看过我的前两篇博客Android插件化(2):OpenAtlas插件安装过程分析和Android插件化(3):OpenAtlas的插件重建以及使用时安装,就知道在插件的安装过程中OpenAtlas做了哪些事,那么插件的卸载就只需要把持久化和内存中的内容移除即可。1.插件的卸载插件卸载的

0评论2017-02-05229

个人简历
吴朝晖/男/1993.1本科/南京师范大学中北学院信息系工作年限:1年以内技术博客:wuzhaohui026.github.ioGitHub:https://github.com/wuzhaohui026期望职位:Android开发(初级Android工程师)期望薪资:税前月薪5.5k~7k期望城市:常州工作经历常州慧展信息科技有

0评论2017-02-05126

Android插件化(五):OpenAtlasの四大组件的Hack
引言到目前为止,我们已经分析了OpenAtlas中插件的安装,卸载,更新,以及安装好插件之后组件类的加载过程,但是对于这些是如何引发的还不知道,比如,在宿主的一个Activit中调用startActivity()跳转到插件中的一个Activity,如何判断这个Activity在的插件是否

0评论2017-02-0598

更多推荐