有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在安卓中压缩文件夹中包含的多个文件而不压缩整个文件夹?

这是我的要求,我有一个文件夹(比如:主文件夹),其中包含三个项目

One folder and two text files I want to zip only these three items contained in the Main folder .Right now I am zipping the contents with the Main folder and the resultant zipped folder name is "temp.zip",when I unzip this,I am getting the "Main folder". But my requirement is when I unzip the "temp.zip",it should display only the contents of the Main folder. Could any one help me in achieving this? Thank you.

编辑:这是我用来压缩文件的代码 这是我正在压缩文件的代码

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

}

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

        }
    }
}                                                                  

私有void addFileToZip(字符串路径、字符串srcFile、ZipOutputStream zip) 抛出异常{

    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 + "/" + folder.getName()));
        while ((len = in.read(buf)) > 0) {
            zip.write(buf, 0, len);
        }

}
}

我使用以下参数调用zipfolder方法: zipFolder(srcpolder,destipath+“/”+“temp.zip”)


共 (4) 个答案

  1. # 1 楼答案

    我刚从这个post抄过来

    根据马特的回答,我成功地使用了这个库

    You can try Zip4j, a pure java library to handle zip file. It supports encryption/decryption of PKWare and AES encryption methods.

    http://www.lingala.net/zip4j/

    Key features:

    • Create, Add, Extract, Update, Remove files from a Zip file
    • Read/Write password protected Zip files
    • Supports AES 128/256 Encryption
    • Supports Standard Zip Encryption
    • Supports Zip64 format
    • Supports Store (No Compression) and Deflate compression method
    • Create or extract files from Split Zip files (Ex: z01, z02,...zip)
    • Supports Unicode file names
    • Progress Monitor

    License:

    • Zip4j is released under Apache License, Version 2.0
  2. # 3 楼答案

    在windows中,可以通过以下步骤实现这一点:

    一,。打开主文件夹,选择要添加到zip文件中的文件 2.右键单击->;添加到archieve 3.选择archieve格式为zip,然后单击“确定”

  3. # 4 楼答案

    try {
        String zipFile = "/locations/data.zip";
        String srcFolder = "/locations";
    
        File folder = new File(srcFolder);
        String[] sourceFiles = folder.list();
    
        //create byte buffer
        byte[] buffer = new byte[1024];
    
        /*
         * To create a zip file, use
         *
         * ZipOutputStream(OutputStream out) constructor of ZipOutputStream
         * class.
         */
        //create object of FileOutputStream
        FileOutputStream fout = new FileOutputStream(zipFile);
    
        //create object of ZipOutputStream from FileOutputStream
        ZipOutputStream zout = new ZipOutputStream(fout);
    
        for (int i = 0; i < sourceFiles.length; i++) {
            if (sourceFiles[i].equalsIgnoreCase("file.csv") || sourceFiles[i].equalsIgnoreCase("file1.csv")) {
                sourceFiles[i] = srcFolder + fs + sourceFiles[i];
                System.out.println("Adding " + sourceFiles[i]);
                //create object of FileInputStream for source file
                FileInputStream fin = new FileInputStream(sourceFiles[i]);
    
                /*
                 * To begin writing ZipEntry in the zip file, use
                 *
                 * void putNextEntry(ZipEntry entry) method of
                 * ZipOutputStream class.
                 *
                 * This method begins writing a new Zip entry to the zip
                 * file and positions the stream to the start of the entry
                 * data.
                 */
    
                zout.putNextEntry(new ZipEntry(sourceFiles[i].substring(sourceFiles[i].lastIndexOf("/") + 1)));
    
                /*
                 * After creating entry in the zip file, actually write the
                 * file.
                 */
                int length;
    
                while ((length = fin.read(buffer)) > 0) {
                    zout.write(buffer, 0, length);
                }
    
                /*
                 * After writing the file to ZipOutputStream, use
                 *
                 * void closeEntry() method of ZipOutputStream class to
                 * close the current entry and position the stream to write
                 * the next entry.
                 */
    
                zout.closeEntry();
    
                //close the InputStream
                fin.close();
    
            }
        }
    
        //close the ZipOutputStream
        zout.close();
    
        System.out.println("Zip file has been created!");
    
    } catch (IOException ioe) {
        System.out.println("IOException :" + ioe);
    }