Android压缩文件访问总结

本文介绍了在Android中如何处理压缩文件,包括读取压缩文件内的文件名、读取特定文件的内容及解压文件的方法。提供了详细的Java代码实现,并针对大文件解压提供了特别处理方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android压缩文件访问总结

        Android压缩文件也是可以通过代码来访问到的,这里使用到两个Zip压缩类,并且只能读取到Zip类型的压缩文件。也可以对文件进行解压,其实就是读取后再写出来。
直接提供代码,都是基于java中IO流的知识了:

一.获取压缩文件内的文件名称

//扫描压缩文件里面的文件名字,传人的是压缩文件的决定路径
    public static void scanZipFile(String zipname) {
        try {
            ZipInputStream zin = new ZipInputStream(
                    new FileInputStream(zipname));
            ZipEntry entry;
            while ((entry = zin.getNextEntry()) != null) {
                //压缩文件里面的文件名称
                System.out.println(entry.getName());
                zin.closeEntry();
            }
            zin.close();
        } catch (IOException e) {
        }
    }

二.获取压缩文件里某一个文件的数据

//扫描压缩文件里面的某一个文件的数据,传人的是压缩文件的决定路径和里面的某一个文件的名称

    public void scanZipFile(String zipname, String name) {
        Log.e("TAG", "scanZipFile");
        try {
            ZipInputStream zin = new ZipInputStream(
                    new FileInputStream(zipname));
            ZipEntry entry;
           while ((entry = zin.getNextEntry()) != null) {
                if (entry.getName().equals(name)) {
            StringBuffer stringBuffer = new StringBuffer();

                    int len = 0;
                    byte[] buf = new byte[1024];
                    while ((len = zin .read(buf)) != -1) {
                        stringBuffer.append(new String(buf, 0, len));
                    }
                    Log.e("TAG", "stringBuffer:" + stringBuffer);
                 }
                zin.closeEntry();
            }
            zin.close();
        } catch (IOException e) {
            Log.e("TAG", e.getMessage());
        }

三.解压文件的代码

    /*
   * 这个是解压ZIP格式文件的方法
   *
   * @zipFileName:是传进来你要解压的文件路径,包括文件的名字;
   *
   * @outputDirectory:选择你要保存的路劲;
   *
   */
    public  static void  unzip(String zipFileName, String outputDirectory)
            throws Exception {
        ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));

        Log.e("TAG", "in==null"+(in==null));

        ZipEntry z;
        String name = "";
        String extractedFile = "";
        int counter = 0;

        while ((z = in.getNextEntry()) != null) {
            name = z.getName();
            Log.d("TAG", "unzipping file: " + name);
            if (z.isDirectory()) {
                Log.d("TAG", name + "is a folder");
                // get the folder name of the widget  
                name = name.substring(0, name.length() - 1);
                File folder = new File(outputDirectory + File.separator + name);
                folder.mkdirs();
                if (counter == 0) {
                    extractedFile = folder.toString();
                }
                counter++;
                Log.d("TAG", "mkdir " + outputDirectory + File.separator + name);
            } else {
                Log.d("TAG", name + "is a normal file");
                File file = new File(outputDirectory + File.separator + name);
                file.createNewFile();
                // get the output stream of the file  
                FileOutputStream out = new FileOutputStream(file);
                int ch;
                byte[] buffer = new byte[1024];
                // read (ch) bytes into buffer  
                while ((ch = in.read(buffer)) != -1) {
                    // write (ch) byte from buffer at the position 0  
                    out.write(buffer, 0, ch);
                    out.flush();
                }
                out.close();
            }
        }

        in.close();

    }

上面三个方法都是测试成功的。失败的话只能是路径不对或没添加权限。
但是第三个方法好像不能解压大文件,
如果是大文件可以使用下面的方法:

四.解压大文件的代码

 /**
     * 2     * 解压缩功能.
     * 3     * 将zipFile文件解压到folderPath目录下.
     * 4     * @throws Exception
     * 5
     */
    public static boolean upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
        //public static void upZipFile() throws Exception{
        ZipFile zfile = new ZipFile(zipFile);
        Enumeration zList = zfile.entries();
        ZipEntry ze = null;
        byte[] buf = new byte[1024];
        while (zList.hasMoreElements()) {
            ze = (ZipEntry) zList.nextElement();
            if (ze.isDirectory()) {
                Log.d("upZipFile", "ze.getName() = " + ze.getName());
                String dirstr = folderPath + ze.getName();
                //dirstr.trim();
                dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
                Log.d("upZipFile", "str = " + dirstr);
                File f = new File(dirstr);
                f.mkdir();
                continue;
            }
            Log.d("upZipFile", "ze.getName() = " + ze.getName());
            OutputStream os = new BufferedOutputStream(new FileOutputStream(getRealFileName(folderPath, ze.getName())));
            InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
            int readLen = 0;
            while ((readLen = is.read(buf, 0, 1024)) != -1) {
                os.write(buf, 0, readLen);
                Log.d("upZipFile", "writing");
            }
            is.close();
            os.close();
        }
        zfile.close();
        Log.d("upZipFile", "finish");
        return true;
    }

    public static File getRealFileName(String baseDir, String absFileName) {
        String[] dirs = absFileName.split("/");
        String lastDir = baseDir;
        if (dirs.length > 1) {
            for (int i = 0; i < dirs.length - 1; i++) {
                lastDir += (dirs[i] + "/");
                File dir = new File(lastDir);
                if (!dir.exists()) {
                    dir.mkdirs();
                    Log.d("getRealFileName", "create dir = " + (lastDir + "/" + dirs[i]));
                }
            }
            File ret = new File(lastDir, dirs[dirs.length - 1]);
            Log.d("upZipFile", "2ret = " + ret);
            return ret;
        } else {
            return new File(baseDir, absFileName);
        }

    }

上面这段代码是可以解压几百兆的压缩文件的,已经试过了。
要添加权限:

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

峥嵘life

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值