1

This is my requirement I have one folder(say: Main folder) which contains three items

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.

Edit :This is the code I am using to zip the files This is the code I am zipping the files

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

        }
    }
}                                                                  

private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception {

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

}
}

I am calling the zipfolder method with these params : zipFolder(srcfolder, destipath + "/" + "temp.zip");

  • @SSS I am using Ubuntu. If zipping multiple files without zipping the entire folder that contains these multiple files is not supported in Ubuntu, its fine if this is supported on Windows. – Gopika Perumalla Sep 24 '12 at 06:44
  • @GopikaPerumalla Can you put up the code that you are using to zip just now? Are you zipping using a stream (like ZipInputStream) or are you using a file based method (like zipfile)? – HaemEternal Sep 24 '12 at 07:24
  • @HaemEternal Please see my edited question I put my code which I am using now to zip the files – Gopika Perumalla Sep 24 '12 at 07:38
  • @GopikaPerumalla Is it simply that you don't want the "Main" folder to appear in your zip? Could you not begin zipping one folder deeper? (Eg in zip folder, work out all of the files and folders in Main, and add each of those into the zip rather than the top folder) – HaemEternal Sep 24 '12 at 07:51
  • @HaemEternal Thanks for the reply,How to zip one folder deeper? – Gopika Perumalla Sep 24 '12 at 09:21
  • @GopikaPerumalla If you are currently calling zipFolder with srcFolder set to (for example) C:\Main; could you not change that to C:\Main\FolderOne? – HaemEternal Sep 24 '12 at 09:24
  • @HaemEternal how to pass more than one srcFolder ? – Gopika Perumalla Sep 24 '12 at 09:33
  • @GopikaPerumalla addFolderToZip("", srcFolderOne, zip); addFolderToZip("", srcFolderTwo, zip); – HaemEternal Sep 24 '12 at 09:44

4 Answers4

0

Zipping a set of individual files into a single zip in Android should be pretty straight forward. There's a pretty good tutorial here that should get you started:

http://www.jondev.net/articles/Zipping_Files_with_Android_%28Programmatically%29

HaemEternal
  • 2,229
  • 6
  • 31
  • 50
0

I just copied from this post

Referred from the matt answer I successfully used this library.

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
Community
  • 1
  • 1
Dev.Sinto
  • 6,802
  • 8
  • 36
  • 54
0
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);
}
mwendamseke
  • 269
  • 2
  • 5
-2

In windows you can achieve this by the following steps,

1.Open the main folder and select the files which you want to add into zip file 2.Right click -> Add to archieve 3.Choose the archieve format as zip and click 'Ok'

SSS
  • 683
  • 1
  • 6
  • 16