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

  • SEARCH
  • Home
  • 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 8593751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:03:34+00:00 2026-06-12T00:03:34+00:00

I am able to zip files contained in a particular folder. This is the

  • 0

I am able to zip files contained in a particular folder. This is the code that I have used:

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

private String[] _files; 
private String _zipFile; 

public Compress(String[] 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]; 

  for(int i=0; i < _files.length; i++) { 
    Log.v("Compress", "Adding: " + _files[i]); 
    FileInputStream fi = new FileInputStream(_files[i]); 
    origin = new BufferedInputStream(fi, BUFFER); 
    ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1)); 
    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(); 
} 

} 

} 

I’m calling this class in this way in another class:

String[] files = {mainFolderPath+"/text1.txt", mainFolderPath+ "/text2.txt", mainFolderPath +"/NewFolder"};
        Compress compress = new Compress(files, sourceFile.getAbsolutePath());
        compress.zip();

On running the application I’m getting an IOException.

Could you please tell me how to zip the “NewFolder” which contains another text file along with the text files “text1.txt” and “text2.txt”?

Thank you.

  • 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-12T00:03:35+00:00Added an answer on June 12, 2026 at 12:03 am

    I got the solution for my question ie; zipping only the contents of a particular folder which includes a folder along with some text files. This is the code:

    public class Compress {
    
    private static final int BUFFER = 2048;
    private String[] _files;
    private String _zipFile;
    
    public Compress(String[] 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];
    
            for (int i = 0; i < _files.length; i++) {
                Log.v("Compress", "Adding: " + _files[i]);
                String name =  _files[i];
                File f = new File( _files[i]);
                if (f.isDirectory()) {
                    name = name.endsWith("/") ? name : name + "/";
    
                    for (String file : f.list()) {
                        System.out.println(" checking " + file);
                        System.out
                                .println("The folder name is: " + f.getName());
                        out.putNextEntry(new ZipEntry(f.getName() + "/" + file));
                        FileInputStream fi = new FileInputStream( _files[i]
                                + "/" + file);
                        origin = new BufferedInputStream(fi, BUFFER);
                        int count;
                        while ((count = origin.read(data, 0, BUFFER)) != -1) {
                            out.write(data, 0, count);
                        }
                        origin.close();
                    }
                    System.out.println(" checking folder" + name);
                } else {
                    FileInputStream fi = new FileInputStream( _files[i]);
                    origin = new BufferedInputStream(fi, BUFFER);
                    ZipEntry entry = new ZipEntry( _files[i].substring( _files[i]
                            .lastIndexOf("/") + 1));
                    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();
        }
    }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Python code similar to this one: for lines in zip(*files): #
I have multiple URLs that returns zip files. Most of the files, I'm able
I have a Dot Net MVC server that stores some zip files. I'm able
I have been able to zip the contents of my folder. But I would
Is there something like a class that might be used to store Files and
I have an app that downloads ZIP files from my server, and can download
I want to be able to take an image that i have already captured
I'm trying to unzip a files that contain other zip files. My attempt is
Possible Duplicate: Appending files to a zip file with Java I have a zip
I'd like to be able to open up (without unzipping) .zip files or .jar

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.