Zipping folders recursively

Hello. I'm new to Java.I have a program that zips a directory recursively, but the problem is with the empty folders.I need to keep the structure of the directory, even if there are empty folders.Can anyone help me, please?
Thank u in advance!
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Zipping {
public static void main(String[] a) throws Exception {
BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
System.out.println("Enter the source directory/file name : ");
String source = in.readLine();
File src = new File (source);
if (src.isDirectory()) {
String zipFile = source + ".zip";
zipFolder (source, zipFile);
else {
int lastDot = source.lastIndexOf(".");
String zipFile;
if (lastDot != -1) {
zipFile = source.substring(0, lastDot) + ".zip";
else {
zipFile = source + ".zip";
zipFolder (source, zipFile);
static public void zipFolder(String srcFolder, String destZipFile) throws Exception {
ZipOutputStream zip = null;
FileOutputStream fileWriter = null;
fileWriter = new FileOutputStream(destZipFile);
zip = new ZipOutputStream(fileWriter);
addFileToZip("", srcFolder, zip);
zip.flush();
zip.close();
static 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);
static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception {
//if ( src.isDirectory()) { 
File folder = new File(srcFolder);
String[] fileName = folder.list();
for (int i = 0; i < fileName.length; i++) {
if (path.equals("")) {
addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
else {
addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName[i], zip);

I hope this is better:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Zipping {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
System.out.println("Enter the source directory/file name : ");
String source = in.readLine();   
File src = new File (source);
if (src.isDirectory()) {
   String zipFile = source + ".zip";
   zipFolder (source, zipFile);
else {
     int lastDot = source.lastIndexOf(".");
     String zipFile;
     if (lastDot != -1) {
        zipFile = source.substring(0, lastDot) + ".zip"; 
     else {
          zipFile = source + ".zip";
zipFolder (source, zipFile);   
static public void zipFolder(String srcFolder, String destZipFile) throws Exception {
    ZipOutputStream zip = null;
    FileOutputStream fileWriter = null;
    fileWriter = new FileOutputStream(destZipFile);
    zip = new ZipOutputStream(fileWriter);
    addFileToZip("", srcFolder, zip);
    zip.flush();
    zip.close();
  static 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);
  static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception {
       File folder = new File(srcFolder);
       String[] fileName = folder.list();
       for (int i = 0; i < fileName.length; i++) {
           if (path.equals("")) {
              addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
else {
addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName[i], zip);

Similar Messages

  • In my firefor options window I have two zipped folders one allows me to brows and see what is inside the other does not give me this option. how can I see what is inside please?

    I have had MyWebSearch install itself as my primary browser....I have needed to reset Firefox in safe mode but I am not sure that the changes have actually been done! In Options in the Options menu the window displays all the add on's and features that are on Firefox....I have two zipped folders here One allows me to see inside the other does not. How do I access this one that does not give me the option to as I want to know what is inside...as this "MyWebSearch has been an enormous hassle...and needless to say has compromised my PC and the security of all of my passwords.....I want to be sure that the resetting the browser default has actually worked and that MyWebSearch is gone!
    Regards
    Coco

    Are you talking about MyWebSearch toolbar (http://help.mywebsearch.com/sbar2.html#q1) which offers apps such as Smiley Central, Cursor Mania???
    If yes, uninstallation instructions are here - (http://help.mywebsearch.com/sbar2.html#q4). Could you provide me some details about the ZIP folders. If I get a screenshot, I can help you very well.

  • Can't figure out why the "Do this automatically for files like this from now on" is disabled Compressed (zipped) Folders

    I recently had to uninstall and reinstall Firefox. Now with the new version it will not allow me to click the check box "Do this automatically for files like this from now on". The line is disabled and a light gray. I went into the Applications tab under options and can't find the Compressed (zipped) Folders file type either.
    I download a bunch of files for work and to have to click is going to be the biggest pain. Any help will be greatly appreciated!!

    Screenshot:

  • How do I disable zip folders in Windows 7?

    I like to use a third-party archive application, so I really don't like how by default Windows makes zip files expand automatically in Windows Explorer and show up in the folder view.
    So... How do I disable zip folders in Windows 7?
    In XP and Vista, I was able to find solutions to remove this "feature" 
    In XP, I simply did...
    regsvr32 /u zipfldr.dll
    In Vista, I had to remove a couple registry entries:
    Windows Registry Editor Version 5.00
    ; Disable ZIP support in Windows Vista
    ; restart Windows after applying this patch
    [-HKEY_CLASSES_ROOT\CLSID\{E88DCCE0-B7B3-11d1-A9F0-00AA0060FA31}]
    [-HKEY_CLASSES_ROOT\CLSID\{0CD7A5C0-9F37-11CE-AE65-08002B2E1262}]
    I don't want to guess and delete the wrong registry entries if someone else knows which ones I need to remove.

    EDIT - I HAVE FOUND A SOLUTION
    After considerable browsing (thank you, Google), I have a solution that works. It is based on fixes for Vista, but works 100% for my Windows 7 64-bit install. 
    You will need to edit your registry (delete 2 different keys), so I highly recommend you back up your registry or better yet, make an image of your OS drive (probably C:\). I use Norton Ghost 2003 (only from a NG 2003 boot floppy) and have never had a single problem. Norton Ghost does not have to be installed on your system for this to work (in fact, it will stall due to compatibility issues). 
    First a note regarding the various reg tweaks that are available for download (e.g., "vistaunzip.reg"). These will NOT work - undoubtedly due to ownership/permissions issues. 
    The following two (2) keys need to be deleted fromthe registry:
    For zip files delete the regkey
    HKEY_CLASSES_ROOT\CLSID\{E88DCCE0-B7B3-11d1-A9F0-00AA0060FA31}
    For cab files delete
    HKEY_CLASSES_ROOT\CLSID\{0CD7A5C0-9F37-11CE-AE65-08002B2E1262}
    When you try to delete them, you will almost certainly get an "access denied" or "failed" type of message. Before you can delete them, you need to change the permissions/ownership of these two keys. Here is how to do it:
    01 - open regedit from the RUN console
    02 - navigate to the first key that needs to be deleted (HKEY_CLASSES_ROOT\CLSID\{E88DCCE0- B7B3-11d1-A9F0-00AA0060FA31})
    03 - right click on the key and select "Permissions"
    04 - click on "Advanced"
    05 - click on the "Ownership" tab
    06 - in the "change owner to" section, highlight your username
    07 - check the box for "replace owner on subcontainers and objects"
    08 - click on "Apply"
    09 - Click "OK"
    10 - Under "Security" tab, highlight your username and check the box for "Allow Full Control" (located mid-menu in the "Permissions For Administrators" section)
    11 - click "Apply" and "OK"
    12 - you can now delete the registry key. MAKE SURE YOU ARE ON THE CORRECT KEY!
    13 - repeat the process for the 2nd key.
    14 - reboot (changes will NOT take effect until you reboot
    15 - VOILA! NO MORE ZIP FOLDERS! woohoo!
    16 - thanks to the original solvers of this problem, whomever they may be.
    I would like the same help that deedvd is seeking. How do I DISABLE the
    zip folder function? I do NOT want my zip files to appear as "folders".
    Why anyone in Redmond thought this was a brilliant idea is beyond me.
    It adds to clutter and makes file browsing a lot less enjoyable. I
    simply want my zip files to appear as files - with the ".zip"
    extension. Just like every other file. The responses to deepdvd were
    probably as frustrating to him as they are to me. Changing the default
    program for zip files is IRRELEVANT. It only changes whether Windows or
    some third party app (7Zip, Izarc, etc) OPENS the files. They continue
    to show up as folders.  I tried the unregister script (earlier thread)
    "Running regsvr32 /u %windir%\system32\zipfldr.dll should be enough to disable zip functionality. Then just run your 3rd party archite application and let it re-register archive files." and it did not work. I got the same error that deeddvd got "The module "C:\Windows\system32\zipfldr.dll" was loaded but the entry-point DllUnregisterServer was not found.
    Make sure that "C:\Windows\system32\zipfldr.dll" is a valid DLL or OCX file and then try again."
    I would love to find a resolution to this problem. If anyone out there knows how to DISABLE the zipfolder function, please post the solution. I am thanking you in advance.

  • Zipped folders are created without inherited permissions

    When I create a zip folder, the zip folder does not inherit permissions are from its parent folder, nor from the files being archived. 
    (Fixing the permissions manually is not a problem -- only an inconvenience -- but this is repetitive.)  I confirmed that the text files, before archiving, have inherited full-control for Everyone from the parent folder. 
    Specific example:
    -- in a folder with inherited full-control for Everyone, I archive text files.
    -- I archive the files by selecting a group of them, right-clicking the selection, hovering over "Send to", and selecting "Compressed (zipped) folder".
    -- The zip folder is created within the same folder... 
    but the zip folder does not allow full-control to Everyone. 
    Why I care: 
    The parent folder is being backed-up using a bat file that executes ROBOCOPY. 
    The robocopy fails to backup the zip sub-folders. 
    I have to edit the permissions on each zip sub-folder after creating it, or set ROBOCOPY parameters to limit retries (in which case zip folders are not backed up). 
    Is there a simple fix, trick, or setting to make "Send to" / "Compressed (zipped) folder" automatically create the zip folder with inherited permissions? 
    (Note:  this problem mostly affects us on one of our "Server 2008 R2" systems, but I have recreated it in Windows 7.) 
    (Also: I posted this question first on answers.microsoft.com, but was told to post it here.) 
    Related question on technet (not yet resolved, as of 9/26/2012) --
    "Compressed (zipped) folder option always created locked zip file" 
    http://social.technet.microsoft.com/Forums/en-US/w7itproui/thread/b83cb991-b554-43a0-9baf-b05a711993ae 
    Doug Ivison

    I realise that this is post has been here for a long time but I happened to be researching something similar and think I have somewhat of a solution for you.
    Send to compressed (zipped) folder inherites permissions from the temp folder as this is the folder that windows uses to make the zip file.
    Try changing the permissions to %USERPROFILE%\AppData\Local\Temp

  • How to create zip folders

    i need to upload a .html doc with image files. Can Firefox create a zip folder and/or convert files to compressed/zip so I can upload as 1 folder? My XP explorer has no option to make a zip folder and I cant afford to buy winzip.

    Firefox and all other browsers can not create zip folders because it is a browser, not a specific zip program.
    An excellent freeware zip program you can download from here:
    http://www.7-zip.org/
    or
    http://www.filehippo.com/download_7zip_32/8754/
    thank you
    Please mark "Solved" the answer that really solve the problem, to help others with a similar problem.

  • How do I archive my verizon email folders into zip folders onto my hard drive?

    I have archived my folders twice before into zip folders on my hard drive by using an option in verizon yahoo mail. The last time was last night. However I can not find this option again. Tech support does not know how.
    Thanks 

    Hello Annie3,
    Verizon Yahoo! mail does not archive using win zip presently.  It appears you were using a 3rd party application to do this which is why your not finding it in the Verizon Yahoo! software.  You can however download the messages into a 3rd party POP client software such as Outlook, Outlook express, Eudora, Thunderbird, Incredimail, etc...  You can find more information on the features of Verizon Yahoo! mail in the help section link http://help.yahoo.com//l/us/verizon/mail/
    Mike_VZ
    Verizon Support
    Notice: Content posted by Verizon employees is meant to be informational and does not supersede or change the Verizon Forums User Guidelines or Terms of Service, or your Customer Agreement Terms and Conditions or Plan.

  • Linking Zip folders for download

    Hi, I want to make my albums available for download with all the MP3s in compressed zip folders...
    I am able to add individual MP3's for download or stream by linking the direct file, however when I compress multiple MP3s into one Zip folder the link is broken and it wont download. I can't seem to find any information about this.
    Im using Dreamweaver CS3 and Mac OSX 10.5.8
    Thanks

    The link should look something like this:
    <a href="photos2009.zip">Download the zip file</a>
    if the zip file is in the root of the directory (with all your other files).
    The zip file needs to be in the folder structure you are working from.
    Highlight the text and use the Link box or the "point to file' icon in the Property Inspector to create the link to the file - the same way you create a link to any other files.
    I do notice that you have spaces in the files name of the zip file.  There should be no spaces in the file name - that could cause a problem.

  • I have been sent a secure CD of zip folders but double clicking only comes up with the message unable to unarchive, error 1 - operation not permitted, any ideas please?

    I have been sent a secure CD of zip folders but double clicking only comes up with the message unable to unarchive, error 1 - operation not permitted, any ideas please?

    First thing coming to mind:
    do you double-click on the zip files on the CD ?
    Maybe the "unzip" tries to write in-place to the CD which of course is not possible.
    Have you tried to copy one of them to your OSX volume/harddisk and thhen double-click the copy ?
    Stefan

  • Compressed Zip folders

    This is a bit of a strange question but is it possible to put compressed zip folders on my iPod. I know I can use it like a memory stick and would like to back up my computers music and pictures to put on my other home computer. It will take ages using only disks. Is this possible???

    I know I can use it like a memory stick and would like
    to back up my computers music and pictures to put on
    my other home computer. It will take ages using only
    disks. Is this possible???
    Of course you can! You'll have to activate the disc mode in the iPods Preferences (within iTunes; Edit->Prefs->iPod).
    Once done, you can access the iPod from "My Computer" as if it was an external HD.
    One more: If the music already is on your iPod, you won't need to put it into a zip-archive, just read this. to learn how to copy the music "backwards" into iTunes.
    BTW: Instead of useing Ottos script (mentioned in that post) you can copy the library files onto the other home computer to save the ratings, playlists and playcounts.
    Hope that helps

  • Is they're a way to look inside zip folders without extracting it?

    Is that possible cause I am thinking of saving space on my HD's by compressing stuff into zip folders then when I want something just extract it without extracting everything?
    Or should I do a compressed disk image? But though I will need it for Windows?

    Oh, sorry, I answered the thread title, and didn't notice that the question you asked was actually different. The 'unzip -l will only list the contents.
    To extract a specific file, use the form:<pre>unzip archive.zip /file/to/extract</pre>
    Now things are actually a bit more complicated than that since HFS+ exended attributes and resource forks are stored separately in the "__MACOSX" folder of the archive in "AppleDouble" format. The File and ._File have to be placed into the same folder and recombined using "/System/Library/CoreServices/FixupResourceForks", but for most files, this extra step won't strictly be necessary.

  • Zipped folders containing InDesign files

    Hi,
    I'm providing advertising material (ads in InDesign) on a FTP server ; the folders containing layouts and links are compressed in .zip.
    Some users can't open the .indd files, it seems they got corrupted, either while the FTP transfer or the zipping process.
    Has anybody heard of trouble with zipped InDesign files ? Should I compress them in .sit instead ??
    Any help greatly appreciated !
    jean-philippe

    One of the best reasons for zipping transferred files (whether transferred by email or FTP) is precisely so that you *know* about file corruption. If your users can extract the .indd file from the zip file, then the zip file made it through intact. If they can't extract the .indd file, then the zip file got corrupted.
    Because your users are able to unzip the .indd file you can be assured that they're getting the same .indd file you put into the zip file. There's some other reason they can't open the .indd files.
    In what way can they not open your .indd files? Do they get an error message?
    Ken

  • Import from folders: recursing fails

    I have just switched from Adobe Lightroom, due to a massive failure on Adobe's part to handle my upgrade to LR3 well. I have been considering trying out Aperture in any case, and this was the final straw.
    So, I need to move all my photos to Aperture. I decided for an open directory hierarchy rather than a managed library, so that I can easily back up and examine my backups. I am not trying to maintain my old structure, but create a new, slightly different one, based on my past experiences.
    - I have in my home account:
    Pictures/OldPhotos/Contax_645Film/2009/06/01/*.tif and so on, many years, months and days, several cameras.
    - I want in my home account:
    Pictures/Photos/2009/06/*.tif etc.
    I tried the following:
    - Open Aperture
    - Import/Folders and Projects... (CmdShiftI)
    - Import Folders as: Folders and Projects
    - Store Files: Pictures (folder in my home account)
    - Copy Files (while testing, eventually move)
    - Ignore Duplicates: Off (my library is empty)
    - Subfolders: Image Year/Month
    - Folder text: (blank)
    - Version Name: CWYYYYMMDD_CAMERACOUNT (COUNT = Aperture counter)
    - CW_(Image Year)(Image Month)(Image Date)_(Custom Name)_(Counter)
    Custom Name = Contax645_Film, Counter = 0 (auto-increment)
    - Apply to Master Filename: On
    - RAW Files Only (there are no JPGs in this camera's folder)
    As far as I understand, this means that Aperture will import the photos in the folder I choose, dump them in the Pictures/Photos folder by year and month, and name them as indicated. It doesn't work, however. I see no photos in the import dialog.
    Then I tried something very similar, this time with normal import:
    - Open Aperture
    - Import (Cmd+I)
    - Destination: 2009 (I created Contax 645 Film/2009 folders in Aperture)
    - Project Name: C645_200906 (I guess I can change this later if needed)
    - Automatically Split Project: Off
    - Do not Import Duplicates: Off
    - Store Files: Pictures (from my user account; Pictures/Photos is autocreated)
    - Copy Files
    - Subfolder: Image Year/Month
    - Add Rename Files section:
    - Version Name: CWYYYYMMDD_CAMERACOUNT (same preset as above)
    - Name Text: Contax645_Film (for Custom Name)
    - Rename Master File (so filename matches version name)
    The elaborate project name lets me click LIBRARY/Projects and see something useful.
    This works if I choose a folder like Pictures/OldPhotos/Contax_645Film/2009/06/01/, but not if I choose Pictures/OldPhotos/Contax_645Film/2009/06, i.e. it fails to recurse. All I have seen indicates that Aperture should recurse into folders while importing, but it doesn't, meaning I have to import again and again, for each day. Argh.
    I have logged a bug with Apple, but thought I would ask here in case anyone knows what I might be doing wrong, or how I can work around it.

    Found what i needed Import - Images does the same job.

  • Sending zip folders to a pc

    When I send a zip folder to other people who use windows they are receiving additional folders marked MAC OSX.Is there a way to avoid this?

    From the Mac Mail menu bar click Edit > Attachments
    Select:  Always Send Windows-Friendly Attachments

  • Zip folders as mail attachments

    I am a new iPad user. I just received pictures in an attached folder in an AOL mail. The folder was zipped and the iPad system will not open it. Any ideas? To get these pictures on the Pad I first opened the folders on my iMac, put the pics in iPhoto and then transferred them to the Pad. That is a lot of work for getting pics on the Pad.

    Try goodreader. It will allow you to open the zip files in the app, unzip them, and save them... Sounds awesome to me!

Maybe you are looking for