压缩/压缩android上的文件夹

时间:2014-03-10 11:26:25

标签: java android

我使用以下代码来压缩我的文件,它工作得很好,但我想只压缩子文件夹,而不是在zip文件中显示树的根。

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”,因为sourcePath X123包含在树中。

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

我想删除X123

 -ZipFile
     -SubFolder1
     -SubFolder2
     -...

谢谢

1 个答案:

答案 0 :(得分:2)

玩过并最终使用以下代码:

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