如何解压缩在android中选择的文件

loading...


0

我知道fileName和path目录所以我想使用uri . zip文件包含图像和另一个文件 . 提前致谢 .
这是我的代码:

//选择一个文件

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                    intent.setType("*/*");
                    // intent.addCategory(Intent.CATEGORY_OPENABLE);

                    try {
                        startActivityForResult(
                                Intent.createChooser(intent, "Select a File to Upload"),
                                FILE_SELECT_CODE);
                    } catch (android.content.ActivityNotFoundException ex) {
                        // Potentially direct the user to the Market with a Dialog
                        Toast.makeText(getActivity(), "Please install a File Manager.",
                                Toast.LENGTH_SHORT).show();
                    }
                }

//得到uri

@Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {  switch (requestCode) {
        case FILE_SELECT_CODE:
            if (resultCode == RESULT_OK) {

         uri = data.getData();

         } break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

//解压缩

private boolean unZip() {

        try {
            String filename;
            String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Test";
          //  InputStream is = new FileInputStream(fullPath + "Test.ojpe");
            ZipInputStream zis = new ZipInputStream(getActivity().getContentResolver().openInputStream(uri));
            ZipEntry mZipEntry;

            byte[] buffer = new byte[1024];
            int count;

            while ((mZipEntry = zis.getNextEntry()) != null) {
                filename = mZipEntry.getName();

                if (mZipEntry.isDirectory()) {
                    File fmd = new File(fullPath + filename);
                    fmd.mkdirs();
                    continue;
                }

                FileOutputStream fout = new FileOutputStream(fullPath + filename);

                // cteni zipu a zapis
                while ((count = zis.read(buffer)) != -1) {
                    fout.write(buffer, 0, count);
                }

                fout.close();
                zis.closeEntry();
                Toast.makeText(getActivity(), "Success", Toast.LENGTH_SHORT).show();
            }

            zis.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

loading...

1回答

  • 0

    我忘了请求许可 . 但是当我选择另一个文件时,它会在解压缩文件中显示以前的图像 . 我能做什么?

评论

暂时没有评论!