如何在Android中将文件压缩到zip文件夹中?[duplicate]

xyhw6mcr  于 2022-11-27  发布在  Android
关注(0)|答案(1)|浏览(163)

此问题在此处已有答案

zip/compress a folder full of files on android(8个答案)
八年前就关门了。
我试图在电子邮件中发送多个文件,我得到了在电子邮件中附加它们的代码。

Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
                       sendIntent.setType("application/octet-stream");
  ArrayList<Uri> uris = new ArrayList<Uri>();
                       uris.add(0, Uri.parse("file://"+Environment.getExternalStorageDirectory()+ "/.file"));
    File f = getActivity().getDatabasePath(".db");
    Log.i(TAG,"DB path"+ f.getAbsolutePath());
    uris.add(1, Uri.fromFile(f));
    sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

但是现在,我需要把它们压缩成一个单独的zip文件,然后我必须在电子邮件中发送它们。我看到了很多答案和想法,但我仍然不能得到一些明确的想法。你们能帮我解决这个问题吗?

rpppsulh

rpppsulh1#

需要这些权限才能将数据存储到设备存储。

主节日.xml文件

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

请求写入外部存储的权限(更新日期:2018/10/04)

public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

Zip函数

public void zip(String[] _files, String zipFileName) {
        try {
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(zipFileName);
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                    dest));
            byte data[] = new byte[BUFFER];
 
            for (int i = 0; i < _files.length; i++) {
                Log.v("Compress", "Adding: " + _files[i]);
                FileInputStream fi = new FileInputStream(_files[i]);
                origin = new BufferedInputStream(fi, BUFFER);
 
                ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1));
                out.putNextEntry(entry);
                int count;
 
                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }
 
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

使用Zip函数

String[] s = new String[2];
 
// Type the path of the files in here
s[0] = inputPath + "/image.jpg";
s[1] = inputPath + "/textfile.txt"; // /sdcard/ZipDemo/textfile.txt
 
// first parameter is d files second parameter is zip file name
ZipManager zipManager = new ZipManager();
 
// calling the zip function
zipManager.zip(s, inputPath + inputFile);

解压缩函数

public void unzip(String _zipFile, String _targetLocation) {
 
        //create target location folder if not exist
        dirChecker(_targetLocatioan);
 
        try {
            FileInputStream fin = new FileInputStream(_zipFile);
            ZipInputStream zin = new ZipInputStream(fin);
            ZipEntry ze = null;
            while ((ze = zin.getNextEntry()) != null) {
 
                //create dir if required while unzipping
                if (ze.isDirectory()) {
                    dirChecker(ze.getName());
                } else {
                    FileOutputStream fout = new FileOutputStream(_targetLocation + ze.getName());
                    for (int c = zin.read(); c != -1; c = zin.read()) {
                        fout.write(c);
                    }
 
                    zin.closeEntry();
                    fout.close();
                }
 
            }
            zin.close();
        } catch (Exception e) {
            System.out.println(e);
        }
}
private void dirChecker(String dir) {
    File f = new File(dir);
    if (!f.isDirectory()) {
            f.mkdirs();
    }
}

使用解压缩功能

ZipManager zipManager = new ZipManager();
zipManager.unzip(inputPath + inputFile, outputPath);

[资料来源:http://stacktips.com/tutorials/android/如何以编程方式压缩和解压缩android中的文件]

相关问题