Android压缩文件列表为ZIP格式

import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtil {
    // 线程池
    private static final ExecutorService ThreadPool = Executors.newCachedThreadPool();

    /**
     * 压缩文件列表到SD
     *
     * @param fileName 压缩文件名称
     * @param pathList 要压缩的文件路径列表
     * @param callback 回调
     */
    public static void ZipFilesToSD(String fileName, List<String> pathList, ZipCallback callback) {
        String zipFilePath = Environment.getExternalStorageDirectory() + File.separator + fileName;
        ZipFiles(zipFilePath, pathList, callback);
    }

    /**
     * 压缩文件列表
     *
     * @param zipFilePath 压缩文件保存路径
     * @param pathList    要压缩的文件路径列表
     * @param callback    回调
     */
    public static void ZipFiles(final String zipFilePath, final List<String> pathList, final ZipCallback callback) {
        ThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFilePath));
                    for (String path : pathList) {
                        File file = new File(path);
                        ZipEntry zipEntry = new ZipEntry(file.getParentFile().getName() + File.separator + file.getName());
                        FileInputStream inputStream = new FileInputStream(file);
                        outZip.putNextEntry(zipEntry);
                        int len;
                        byte[] buffer = new byte[4096];
                        while ((len = inputStream.read(buffer)) != -1) {
                            outZip.write(buffer, 0, len);
                        }
                        outZip.closeEntry();
                    }
                    outZip.finish();
                    outZip.close();
                    callback.onSuccess(zipFilePath);
                } catch (IOException e) {
                    e.printStackTrace();
                    callback.onFailed(e);
                }
            }
        });
    }

    /**
     * 回调接口
     */
    public interface ZipCallback {
        void onSuccess(String zipFilePath);

        void onFailed(Exception e);
    }
}

参考链接:

Android文件或文件夹压缩成.zip格式的压缩包

三种方法把文件读成一个字符串

节选自:三种方法把文件读成一个字符串


//Example 1
 
//Read file content into string with - Files.lines(Path path, Charset cs)
 
private static String readLineByLineJava8(String filePath)
{
    StringBuilder contentBuilder = new StringBuilder();
    try (Stream<String> stream = Files.lines( 
                                     Paths.get(filePath), StandardCharsets.UTF_8))
    {
        stream.forEach(s -> contentBuilder.append(s).append("\n"));
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return contentBuilder.toString();
}
 
//Example 2
 
//Read file content into string with - Files.readAllBytes(Path path)
 
private static String readAllBytesJava7(String filePath)
{
    String content = "";
    try
    {
        content = new String ( Files.readAllBytes( Paths.get(filePath) ) );
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return content;
}
 
//Example 3
 
//Read file content into string with - using BufferedReader and FileReader
//You can use this if you are still not using Java 8
 
private static String usingBufferedReader(String filePath)
{
    StringBuilder contentBuilder = new StringBuilder();
    try (BufferedReader br = new BufferedReader(new FileReader(filePath)))
    {
 
        String sCurrentLine;
        while ((sCurrentLine = br.readLine()) != null)
        {
            contentBuilder.append(sCurrentLine).append("\n");
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return contentBuilder.toString();
}

Android 系统文件限制小探

节选自:Android 系统文件限制小探


在Linux系统中,每个进程可以使用的FD数量是有上限的,在Android中这个上限为1024,表示每个进程可以创建的file descriptors 不能超多1024个。当系统某一文件的打开句柄数超过1024时,就会报错:

"Too many open files"

修改系统最大打开文件数量:

在init.rc中增加:
setrlimit 7 1048 1048