java - 压缩/压缩android上的文件夹

标签 java android

我使用以下代码来压缩我的文件并且效果很好,但我只想压缩子文件夹而不是在压缩文件中显示树的根。

public boolean zipFileAtPath(String sourcePath, String toLocation) {
// ArrayList<String> contentList = new ArrayList<String>();
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;
}

private void zipSubFolder(ZipOutputStream out, File folder,
    int basePathLength) throws IOException {
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();
    }
}
}

public String getLastPathComponent(String filePath) {
String[] segments = filePath.split("/");
String lastPathComponent = segments[segments.length - 1];
return lastPathComponent;
}

现在,如果我输入 Environment.getExternalStorageDirectory().toString()+"/X123",因为源路径 X123 包含在树中。

-ZipFile
     -X123
          -SubFolder1
          -SubFolder2
          -...

我想删除 X123

 -ZipFile
     -SubFolder1
     -SubFolder2
     -...

谢谢

最佳答案

反复尝试并最终使用以下代码:

static public void zipFolder(String srcFolder, String destZipFile)
        throws Exception {
    ZipOutputStream zip = null;
    FileOutputStream fileWriter = null;
    fileWriter = new FileOutputStream(destZipFile);
    zip = new ZipOutputStream(fileWriter);
    addFolderToZip("", srcFolder, zip);
    zip.flush();
    zip.close();
}



static private void addFileToZip(String path, String srcFile,
        ZipOutputStream zip) throws Exception {
    File folder = new File(srcFile);
    if (folder.isDirectory()) {
        addFolderToZip(path, srcFile, zip);
    } else {
        byte[] buf = new byte[1024];
        int len;
        FileInputStream in = new FileInputStream(srcFile);
        zip.putNextEntry(new ZipEntry(path.replace("X123/", "") + "/" + folder.getName()));
        //zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
        while ((len = in.read(buf)) > 0) {
            zip.write(buf, 0, len);
        }
    }
}

static private void addFolderToZip(String path, String srcFolder,
        ZipOutputStream zip) throws Exception {
    File folder = new File(srcFolder);
    for (String fileName : folder.list()) {
        if (path.equals("")) {
            addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
        } else {
            addFileToZip(path + "/" + folder.getName(), srcFolder + "/"
                + fileName, zip);
        }
    }
}

关于java - 压缩/压缩android上的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22298851/

相关文章:

java - 如何在ubuntu中从java程序运行c可执行文件

java - Java 的日期格式

java - mvn clean package error : No compiler is provided in this environment. 也许您正在 JRE 而不是 JDK 上运行?

java - Android Studio Gradle CreateProcess 错误=2

java - Android开发类(class)组织

android - 服务到 Activity 粘性通信

java - 将字符串从 Activity 传递到 fragment

java - JPA 2.1 StoredProcedureQuery 与 PostgreSQL 和 REF_CURSORs

安卓工作室 1.5.1 : Could not find property 'vectorDrawables'

android - 清除 map 标记,但我所在位置的标记除外