Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8124711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:33:08+00:00 2026-06-06T06:33:08+00:00

I want zip up a folder structured like so: temp/folder1/file1 temp/folder2/file2 temp/file3 and maintain

  • 0

I want zip up a folder structured like so:

temp/folder1/file1

temp/folder2/file2

temp/file3

and maintain the directory structure exactly.
Currently, when I zip it, I get a zip that doesnt maintain the directory structure. It looks like this
file1
file2
file3

what do I do to add the files in their respective folders like all the normal zipping applications do?

This is the code I have 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, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }

                origin.close();
            }

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

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:33:10+00:00Added an answer on June 6, 2026 at 6:33 am

    I am not android developer so consider this code as a TIP only

    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 am 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 path to file leaving just file name (something like _files.get(i).getName()). If that is true then that is why you don’t have folder structure in your zip file. You are saying that zip entry should be just that file name, without any folders.

    So if you want zip file to contain some part of path from
    /my/full/path/to/folder/temp/folder1/file1 like temp/folder1/file1 just remove unnecessary part of that path when you create 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, ""));
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose the structure: /foo/bar/ --file1 --file2 --file3 --folder1 --file4 --folder2 --file5 I want to
I want zip folder through my console application that's why I used something like
Visual Studio Templates has folder structure like this: /ProjectTemplatesCache /CSharp /Windows /1033 /ClassLibrary.zip --
I want to copy the files from the zip folder to another folder. I
I want to pack to zip some folder using SharpZipLib. Example stucture directory1: directory2:
I want to zip a folder containing files. So inorder to do that i
hi i 've following code. and i want to delete a zip folder from
I want to extract some files. Ex. test.zip to /path/to/folder. Using Archive::Extract and specifying
I have a directory that contains several files. I want compress this folder to
I have 10 zip files in a folder having path like this TargetDirectory =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.