android - 如何将文件压缩到android中的zip文件夹中?

标签 android zip compression

<分区>

我正在尝试通过电子邮件发送多个文件,我得到了将它们附加到电子邮件中的代码。

   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 文件,然后我必须通过电子邮件发送它们。好吧,我看到了很多答案和想法,但我仍然无法得到一些清晰的想法。你们能帮我解决这个问题吗。

最佳答案

将数据存储到您的设备存储需要这些权限。
Mainfest.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
        );
    }
}

压缩功能

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();
        }
    }

使用压缩功能

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/how-to-programmatically-zip-and-unzip-file-in-android]

关于android - 如何将文件压缩到android中的zip文件夹中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25562262/

相关文章:

java - 在java中压缩和解压缩大尺寸数据?

android - NPE 在 eclipse 期间从 "Initializing Java Tooling"开始

android - 在 uiautomator 中滑动到下一页

php - 安装 PHP Zip 扩展

java - 从 InputStream 创建 Java 7 zip 文件系统

java - 字符串和符号的压缩

android - "Glooming"ListView底部效果层

android - 如何在新的自定义 View 中支持 "tools:text"?

java - 如何从客户端计算机下载zip文件而不是使用java在浏览器中打开文件?

c - 如何修改这个字符串压缩C代码来处理超过9次的字符重复?