How to maintain the folder structure when compressing in Java?

I want to compress a folder with a structure like this:

Temperature / folder 1 / file1

Temperature / Folder 2 / File2

Temperature / file 3

And completely maintain the directory structure At present, when I zip, I get a zip that doesn't maintain the directory structure Looks like this file 1 file 2 file 3

How to add files to their respective folders, just like all normal compression applications?

This is my code so far:

package com.damastah.deflash;

import android.util.Log;
import java.io.BufferedInputStream;
import java.io.bufferedoutputstream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Compress {
    private static final int BUFFER = 2048;

    private ArrayList<File> _files;
    private String _zipFile;

    public Compress(ArrayList<File> files,String zipFile) {
        _files = files;
        _zipFile = zipFile;
    }

    public void zip() {
        try {
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(_zipFile);

            ZipOutputStream out = new ZipOutputStream(new bufferedoutputstream(
                    dest));

            byte data[] = new byte[BUFFER];
            Log.e("Compress - zip","test");
            for (int i = 0; i < _files.size(); i++) {
                Log.v("Compress","Adding: " + _files.get(i).getAbsolutePath());
                FileInputStream fi = new FileInputStream(_files.get(i)
                        .getAbsolutePath());
                origin = new BufferedInputStream(fi,BUFFER);
                ZipEntry entry;
                if (_files.get(i).getAbsolutePath().contains("."))
                    entry = new ZipEntry(_files
                            .get(i)
                            .getAbsolutePath()
                            .substring(
                                    _files.get(i).getAbsolutePath()
                                            .lastIndexOf("/") + 1));
                else
                    entry = new ZipEntry(_files.get(i).getAbsolutePath());
                out.putNextEntry(entry);

                int count;
                while ((count = origin.read(data,BUFFER)) != -1) {
                    out.write(data,count);
                }

                origin.close();
            }

            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Solution

I'm not an Android Developer, so think of this code as just a hint

class ZipCompress {
    public static void main(String[] args) throws IOException {

        String dir = "d:\\My folder\\";
        // this entries I want to put in archives in the way they are Now
        String[] entries = { "temp\\folder1\\file1.txt","temp\\folder2\\file2.txt","temp\\file3.txt" };

        ZipOutputStream zipos = new ZipOutputStream(new bufferedoutputstream(
                new FileOutputStream(dir + "archive.zip")));

        // time to create entries and fill them with data
        for (String entrie : entries) {
            System.out.println("Writing file: " + dir + entrie);

            // prepering file stream
            BufferedInputStream fileStream = new BufferedInputStream(
                    new FileInputStream(dir + entrie));

//--------------------------------------------------------------------------
//          Here we decide how we entry will look like in archive.
//          We use only part of path from String[] entries
//          like "temp\\folder1\\file1.txt"
//--------------------------------------------------------------------------
            ZipEntry newEntry = new ZipEntry(entrie);

            newEntry.setComment("comment to entry: " + newEntry);

            // Now lets put this entry in archive
            zipos.putNextEntry(newEntry);

            // lets put data from file to current archive entry
            int c;
            while ((c = fileStream.read()) != -1)
                zipos.write(c);
            fileStream.close();
        }
        zipos.close();
    }
}

edit

I don't know what you want to do here

if (_files.get(i).getAbsolutePath().contains("."))
                    entry = new ZipEntry(_files
                            .get(i)
                            .getAbsolutePath()
                            .substring(
                                    _files.get(i).getAbsolutePath()
                                            .lastIndexOf("/") + 1));

From what I see, it deletes the path of the file, leaving only the file name (similar to _files. Get (I)) getName()). If this is true, then this is why there is no folder structure in your zip file You said that the zip entry should only be the file name without any folder

Therefore, if you want the zip file to contain some parts of the path / my / full / path / to / folder / temp / folder1 / file1 like temp / folder1 / file1, delete only the unnecessary parts of the path when creating the zipentry

String dir = "/my/full/path/to/folder/";
for (int i = 0; i < _files.size(); i++) {
...
    if (_files.get(i).getAbsolutePath().contains(".")) {
        entry = new ZipEntry(_files
            .get(i).getAbsolutePath().replace(dir,""));
...
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>