Android 压缩资源文件夹中的资源和/或文件的方法?

Android 压缩资源文件夹中的资源和/或文件的方法?,android,memory,compression,assets,Android,Memory,Compression,Assets,我快要完成我的申请了。我的资产文件夹中有20多个PDF文件,显然它们占用了相当多的空间。有没有办法压缩这些文件以使应用程序更小?在我的代码中/整个项目中,还有哪些检查/降低内存消耗的好方法?提前感谢您的任何意见 您可以压缩文件,然后在需要时解压缩 这里有一个教程 检查,它提供压缩和解压缩的zip和gzip功能 安装应用程序后,您还可以从其他站点下载PDF 或者这应该有效: /* * * Zips a file at a location and places the resulti

我快要完成我的申请了。我的资产文件夹中有20多个PDF文件,显然它们占用了相当多的空间。有没有办法压缩这些文件以使应用程序更小?在我的代码中/整个项目中,还有哪些检查/降低内存消耗的好方法?提前感谢您的任何意见

您可以压缩文件,然后在需要时解压缩

  • 这里有一个教程
  • 检查,它提供压缩和解压缩的zip和gzip功能
  • 安装应用程序后,您还可以从其他站点下载PDF
  • 或者这应该有效:

        /*
     * 
     * Zips a file at a location and places the resulting zip file at the toLocation
     * Example: zipFileAtPath("downloads/myfolder", "downloads/myFolder.zip");
     */
    
    public boolean zipFileAtPath(String sourcePath, String toLocation) {
        // ArrayList<String> contentList = new ArrayList<String>();
        final int BUFFER = 2048;
    
    
        File sourceFile = new File(sourcePath);
        try {
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(toLocation);
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                    dest));
            if (sourceFile.isDirectory()) {
                zipSubFolder(out, sourceFile, sourceFile.getParent().length());
            } else {
                byte data[] = new byte[BUFFER];
                FileInputStream fi = new FileInputStream(sourcePath);
                origin = new BufferedInputStream(fi, BUFFER);
                ZipEntry entry = new ZipEntry(getLastPathComponent(sourcePath));
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
            }
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    
    /*
     * 
     * Zips a subfolder
     * 
     */
    
    private void zipSubFolder(ZipOutputStream out, File folder,
            int basePathLength) throws IOException {
    
        final int BUFFER = 2048;
    
        File[] fileList = folder.listFiles();
        BufferedInputStream origin = null;
        for (File file : fileList) {
            if (file.isDirectory()) {
                zipSubFolder(out, file, basePathLength);
            } else {
                byte data[] = new byte[BUFFER];
                String unmodifiedFilePath = file.getPath();
                String relativePath = unmodifiedFilePath
                        .substring(basePathLength);
                Log.i("ZIP SUBFOLDER", "Relative Path : " + relativePath);
                FileInputStream fi = new FileInputStream(unmodifiedFilePath);
                origin = new BufferedInputStream(fi, BUFFER);
                ZipEntry entry = new ZipEntry(relativePath);
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }
        }
    }
    
    /*
     * gets the last path component
     * 
     * Example: getLastPathComponent("downloads/example/fileToZip");
     * Result: "fileToZip"
     */
    public String getLastPathComponent(String filePath) {
        String[] segments = filePath.split("/");
        String lastPathComponent = segments[segments.length - 1];
        return lastPathComponent;
    }
    
    /*
    * 
    *在某个位置将文件拉入拉链,并将生成的zip文件放置在该位置
    *示例:zipFileAtPath(“downloads/myfolder”、“downloads/myfolder.zip”);
    */
    公共布尔zipFileAtPath(字符串源路径、字符串位置){
    //ArrayList contentList=新的ArrayList();
    最终整数缓冲区=2048;
    文件sourceFile=新文件(sourcePath);
    试一试{
    BufferedInputStream原点=null;
    FileOutputStream dest=新FileOutputStream(toLocation);
    ZipOutputStream out=新ZipOutputStream(新缓冲输出流(
    目的地);
    if(sourceFile.isDirectory()){
    zipSubFolder(out,sourceFile,sourceFile.getParent().length());
    }否则{
    字节数据[]=新字节[缓冲区];
    FileInputStream fi=新的FileInputStream(sourcePath);
    原点=新的BufferedInputStream(fi,缓冲区);
    ZipEntry条目=新ZipEntry(getLastPathComponent(sourcePath));
    外出、外出(进入);
    整数计数;
    while((count=origin.read(数据,0,缓冲区))!=-1){
    输出。写入(数据,0,计数);
    }
    }
    out.close();
    }捕获(例外e){
    e、 printStackTrace();
    返回false;
    }
    返回true;
    }
    /*
    * 
    *打开子文件夹
    * 
    */
    专用void ZipusubFolder(ZipoutStream out,文件文件夹,
    int basePathLength)引发IOException{
    最终整数缓冲区=2048;
    File[]fileList=folder.listFiles();
    BufferedInputStream原点=null;
    用于(文件:文件列表){
    if(file.isDirectory()){
    zipSubFolder(输出、文件、基本路径长度);
    }否则{
    字节数据[]=新字节[缓冲区];
    字符串unmodifiedFilePath=file.getPath();
    String relativePath=未修改的文件路径
    .子串(基路径长度);
    Log.i(“ZIP子文件夹”,“相对路径:“+relativePath”);
    FileInputStream fi=新的FileInputStream(未修改的文件路径);
    原点=新的BufferedInputStream(fi,缓冲区);
    Zippentry条目=新Zippentry(相对路径);
    外出、外出(进入);
    整数计数;
    while((count=origin.read(数据,0,缓冲区))!=-1){
    输出。写入(数据,0,计数);
    }
    origin.close();
    }
    }
    }
    /*
    *获取最后一个路径组件
    * 
    *示例:getLastPathComponent(“下载/Example/fileToZip”);
    *结果:“fileToZip”
    */
    公共字符串getLastPathComponent(字符串文件路径){
    String[]segments=filePath.split(“/”);
    字符串lastPathComponent=segments[segments.length-1];
    返回lastPathComponent;
    }