How to save folder structure when zipping in Java?

I want to create a folder structured like this:

TEMP / folder1 / file1

TEMP / folder2 / file2

TEMP / file3

and accurately maintain the directory structure. Currently, when I zip it, I get a zip that does not support the directory structure. It looks like this is file1 file2 file3

What should I do to add files to the appropriate folders, for example, all the usual applications for zipping?

This is the code that I still have:

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, 0, BUFFER)) != -1) { out.write(data, 0, count); } origin.close(); } out.close(); } catch (Exception e) { e.printStackTrace(); } } } 
+4
source share
1 answer

I am not an Android developer, therefore, we consider this code only as TIP

 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'm not sure 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 removes the path to the file, leaving only the file name (something like _files.get(i).getName() ). If this is true, that is why you do not have the folder structure in your zip file. You say that the zip entry should only be that file name without any folders.

So, if you want the zip file to contain part of the path from /my/full/path/to/folder/temp/folder1/file1 as temp/folder1/file1 just delete the unnecessary part of this path when creating the ZipEntry, for example

 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, "")); ... 
+1
source

All Articles