Problems unzipping files

Hello there,
I tried to create two small programs: Zip and UnZip, using the java.util.zip package. The Zip file works perfectly and WinAce can read the files zipped. But the UnZip program doesn't seem to work: it unzips
the files almost correctly. I unzipped a text file and it's readable, but except for the normal characters that appeared in the original text, some other not-displayable characters appear at certain points (quite many since the unzipped files are a few kilob bigger that the original not-zipped file). I really don't understand what the problem is. Well, actually, I didn't read any tutorial about using java.util.zip, I just read the API documentation and I sorted out a way of zipping and unzipping. Please, if you can, tell me what is the problem with the program:
public class UnZip
     public static void main(String[] arg)
          try
               if (arg.length < 1)
                    System.out.println("Usage: java UnZip <zip file> [to dir]");
                    System.exit(0);
               String dir = "";
               if (arg.length > 1)
                    dir = arg[1] + System.getProperty("file.separator");
                    File f = new File(arg[1]);
                    if (!f.exists()) f.mkdirs();
               System.out.println("Decompressing files from file " + arg[0]);
               ZipFile read = null;
               try
                    read = new ZipFile(arg[0]);
               catch (ZipException ex)
                    System.err.println("Zip error when reading file " + arg[0]);
                    System.exit(1);
               catch (IOException ex)
                    System.err.println("I/O Exception when reading file " + arg[0]);
                    System.exit(1);
               Enumeration en = read.entries();
               while (en.hasMoreElements())
                    ZipEntry entry = (ZipEntry) en.nextElement();
                    System.out.println("\tDecompressing file " + entry.getName());
                    FileOutputStream out;
                    try
                         out = new FileOutputStream(dir + entry.getName());
                    catch (FileNotFoundException ex)
                         System.err.println("\tCannot write down to file " + dir + entry.getName());
                         continue;
                    InputStream re = read.getInputStream(entry);
                    int av = 0;
                    while ((av = re.available()) != 0)
                         byte[] b = new byte[av < 64 ? av : 64];
                         re.read(b);
                         out.write(b);
                    out.close();
                    System.out.println("\tDone decompressing file " + entry.getName());
               read.close();
               System.out.println("Done decompressing files");
          catch (Exception e)
               new File(arg[1]).delete();
               e.printStackTrace(System.err);
}

It is the following code which has a swapped conditional expression,
and also I would loop differently.
          int av = 0;
          while ((av = re.available()) != 0)
               byte[] b = new byte[av < 64 ? av : 64];
               re.read(b);
               out.write(b);
          }Move the byte buffer outside, and better think in kilobytes.
Read is waiting and forcing an actual read, so it seems
safer than an available.
byte[] buffer = new byte[1024];
for (;;) {
    int nread = re.read(buffer);
    if (nread < 0)
        break;
    out.write(buffer, 0, nread);

Similar Messages

  • Help - problem unzipping files on Terminal

    Hi,
    I have been unzipping files (zip and rar) using Terminal for quite sometimes now. For unknown reasons, it's given me some problems unzipping today.
    When I typed cd. /desktop, as I did in the past, it suddenly showed an error:
    -bash: -bash:: command not found
    I typed "/bin/echo $PATH", as I saw other previous posts have suggested. The result:
    /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
    I am not sure what that means, i.e. is it ok? or is it not? and how do I fix the problem if not ok?
    I am completely IT illiterate, especially when it comes to any computer language. So please, a simple and straight forward answer, or preferrably a step-by-step guide, will be truly appreciated.
    Many thanks for your kind assistance in advance.

    MrHoffman, thanks ever so much for the helpful answer.
    a "cd ~/Desktop" instead of "a dot and a space" did allow me to unzip my files finally!
    Just wonder, by doing what I have just done, i.e. a dot and a space and then the target directory, had I already committed the sins of damaging the "Path" (something I learned from reading other similar posts in the forum)... Basically, I just want to know if i have damaged the file contents... and if so, how bad is the damage? (so far, my Mac works fine....)
    Digress, I would have loved to stay away from using Terminal as far away as I could, since I am dreading of the thought of me ended up doing something damaging to the lovely Mac. But unfortuantely, there isn't any idiot-friendly tool available for unzipping any password-protected zip files.... Would appreciate if you could offer any alternative solution to this, i.e. other than using Terminal! (NB. I have also looked into using EZ 7z and X7zX for Mac before, both of which i just gave up in the end!)
    Anyhow, thanks! : )

  • Problems unzipping files downloaded from Firefox using Windows built-in extractor tool

    Hi,
    I am running Firefox v26 on a Windows 7 (64-bit) PC. I work for a library that is able to reproduce digital audio books that we can download from the Library of Congress's National Library Service (NLS) for the Blind and Physically Handicapped. I use Firefox to download a lot of zipped audio books for my job. The file formats found in one book's zipped folder often include the following file types: .3gp (they're like a more compressed MP3 audio file), .mp3, .ncx, .opf, .pncx, .ppf, .smil, .md5, .dtd, .ent, and .ao.
    If I use Windows' built-in extraction tool to unzip an audio book I have downloaded via Firefox the extraction process ends prematurely and acts as if it was successful, but in reality some files are were never extracted. For example, I downloaded a zipped audio book that contained 21 files and weighed in at 87.6MB. The built-in Windows extraction tool only unzipped 2 files weighing in at 30.3MB.
    I have downloaded this same zipped audio book using Google's Chrome v31 and IE 11. Both downloads unzipped successfully using Windows' extractor. To make things even more bizarre, I *was* able to successfully unzip my audio book that I downloaded using Firefox using a 3rd party extraction tool called Extract Now. Because I tend to use this 3rd party program to extract my audio files I don't know at which version of Firefox this problem started. All I know if that this hasn't always been the case.
    Please help.
    Thanks,
    -Dan M.

    Boot the computer in Windows Safe Mode with network support (press F8 on the boot screen) as a test to see if that helps with extracting files.

  • Problem unzip file (Solved)

    When I go to uncompress a .rar file with nemo, nautilus I get this error.
    Error: Parsing filters is unsupported
    It seems to be a problem with libarchive , I'm not sure , but I can not find a solution.
    Any idea
    Thanks
    Last edited by kabbalah (2015-04-24 01:51:05)

    Is it another symptom of this issue https://bbs.archlinux.org/viewtopic.php?id=196198
    Does the PKGBUILD with patch from that thread fix your issue?

  • Problems Unzipping file on my desktop

    When I try to unzip a file, I get a message: "Unable to unarchive "name of file" into "Desktop". (Error 1 - Operation not permitted.)". The file is 9.3 Gb, but I have opened files exactly like this in the recent past. Any suggestions?
    I am using mac os 10.6.8.

    How are the permissions of your file? How is the extension of the file? Is it .zip ?
    Could you post back the result of (copy and paste in your Terminal of these comands followed by <enter>):
    cd ~/Desktop
    ls -l
    Is this command in the Terminal working:
    unzip yourfile.zip
    Can you move the file to an other folder of your home directory?
    Best greetings
    marek

  • Problem with unzipping file

    I am trying to unzip a file using java.util.zip. It is throwing a ZipException in the first line where I instantiate ZipFile object.
    ZipFile oFile = new ZipFile("/myzip.zip");java.util.jar is not working either. It fails right here:
    JarFile oFile = new JarFile("/myzip.zip");Here is the exception thrown in the above cases:
    java.util.zip.ZipException: error in opening zip file
            at java.util.zip.ZipFile.open(Native Method)
            at java.util.zip.ZipFile.<init>(Unknown Source)
            at java.util.jar.JarFile.<init>(Unknown Source)
            at java.util.jar.JarFile.<init>(Unknown Source)
            at ReadZipFile.main(ReadZipFile.java:21)
    An interesting observation:
    I am able to extract the contents of the zip from the command-line using jar utility.
    jar xvf myzip.zipI am stumped! I think jar utility internally uses the same classes for unzipping files. How come it is working with jar utility whereas it is not working from Java program? Also, the myzip.zip appears to be a valid archive - I am able to open it with winzip.
    Any pointers will be appreciated.
    Thanks in advance,
    Ganesh

    I think the above reply is correct but didn't explain why you would be getting a file not found. Unless you have your zip file at the root of your harddrive the slash in the filename is improper. Without the slash your code will assume your current working directory and look for the file there.
    By putting that forward slash in there you are telling the code to look at the root of the filestructure and NOT at the current working directory.

  • Unzip file using PayloadZipBean in Sender FILE adapter

    I am trying to unzip a flat file in Sender FILE adapter using PayloadZipBean.
    I am getting mapping error. The problem is File adapter is reading data without unzipping. I am getting weird characters/data, so mapping error.
    I searched all the blogs/ forum threads and help.sap.com, everywhere the focus is on zipping, not unzipping.
    the file I have is with .gz extension, but I tried with .zip extension also.
    Please help in resolving this issue.
    Edited by: Gopal Janagama on May 2, 2008 11:42 PM

    Gopal,
    GZIP and ZIP format are slightly different, as stated here :
    "GZIP compresses only one file and does not have a header. ZIP contains header information about what files are contained in the file."
    So I'm not sure the PayloadZipBean supports GZIP format ... Try to ungzip and then zip its content and see if the bean works for it
    Rgds
    Chris

  • Unzipping Files in the wrong folder

    Hi
    I am trying to unzip files. It works fine except that it unzips the files at the root. So i have tried specifying the path for making the directory (in which the unzipped files are stored). But that doesnt seem to work. Now i am unable to find the unzipped files. CAN ANYONE HELP PLEASE
    public static void unzipFile(String zFile)
            Enumeration entries;
            ZipFile zipFile;
           try {
                zipFile = new ZipFile(zFile);
                File file = new File(zipFile.getName());
                String fname = file.getName();
               String substr1 = fname.substring(0, fname.indexOf("."));
                String filepath = "/data/psawant/temp";
                File dir = new File(filepath, substr1);
                try {
                        dir.mkdir();
                catch (Exception fe)
                      System.err.println("Unhandled exception:");
                        fe.printStackTrace();
                        return;
                entries = zipFile.entries();
                while(entries.hasMoreElements()) {
                    ZipEntry entry = (ZipEntry)entries.nextElement();
                    if(entry.isDirectory())
                         // Assume directories are stored parents first then children.
                        System.err.println("Extracting directory: " + entry.getName());
                                          (new File(entry.getName())).mkdir();
                        continue;
                    } //end of if
                    System.err.println("Extracting file: " + entry.getName());
                    copyInputStream(zipFile.getInputStream(entry),
                        new BufferedOutputStream(new FileOutputStream(entry.getName())));
                 } //end of while
                zipFile.close();
                   } catch (IOException ioe)  {                 }
             } //end of unzip function
    public static final void copyInputStream(InputStream in, OutputStream out)
        throws IOException
            byte[] buffer = new byte[1024];
            int len;
            while((len = in.read(buffer)) >= 0)
            out.write(buffer, 0, len);
            in.close();
            out.close();
    Any help is apprciated
    Thanx

    Sorry I got it this time. I had overlooked the new File. I added that & it works fine but there seems to be some problem
    If the folder has another folder inside . it doesnt show that one. or rather just unzips one folder & no folders inside it. But i guess i can check for that
    Thanx for u r help
    Have a nice day

  • UNZIP file from ABAP

    Hi,
    I have a requirement to retrieve a zip file from the FTP Server, unzip it to the SAP Server and process the unzipped files. Retrieving the data from the FTP server should work via FTP commands which can be executed from within an ABAP program. However, the problem is unzipping these files via ABAP and put them on the server as well. 1. How to do this from within an ABAP Program? (unix commands?)
    2. Can I unzip these files with or without using the folder structure? (additional parameter for unix command?)
    Kind regards,
    Micky.

    REPORT ZTESTUNIX line-size 400
                    no standard page heading.
    selection-screen begin of block ucmd with frame title text-001.
    parameters: unixcom like   rlgrap-filename.   " ...SAP Interface file
    selection-screen end of block ucmd.
    data: begin of tabl occurs 500,
            line(400),
          end of tabl.
    data: lines type i.
    start-of-selection.
      refresh tabl.
      call 'SYSTEM' id 'COMMAND' field unixcom
                    id 'TAB'     field tabl[].
      describe table tabl lines lines.
      loop at tabl.
        write:/01 tabl-line.
      endloop.
      skip 2.
      if lines = 0.
        write:/ 'NO Occurances were found'.
      else.
        write:/ 'Command was successfully executed' color col_total.
        write:/ 'Number of entries in Search' color col_total,
                 lines color 6.
      endif.
    end-of-selection.
    while running give the command as
    <b>unzip test.zip  -x dir</b>
    then it will unzip test.zip file to dir called dir.
    you can specify the filepath instead of file...
    regards
    vijay

  • Problems opening files

    This question is probably basic and i should know the answer but can't figure it out. so here i go. it is a two part question or maybe 2 different questions all together:
    1: I have some files that will open in preview but when i change it it just doesn't happen. (.psd, .pdf, .jpg, .jpeg etc.) i want to change it for all programs so when i click on a .pdf it opens acrobat instead of preview. and photoshop instead of preview (.psd) somehow this doesn't work at all!!!
    2. for photoshop: i right click on the file, open with adobe photoshop it will open photoshop but not the file itself. any one have this same problem? anyone know of any fixes??? i am getting frustrated cause i then have to open the file manually with he file menu. this has also happened with some other programs as well. any help would be excellent!!!!
    thanks.
    stefan

    The Photoshop file problem means that this is missing:
    "/Library/ScriptingAdditions/Adobe Unit Types"
    Last I heard Abode was still recommending that you reinstall Photoshop, which is insane. You can download the Adobe Unit Types file here:
    http://homepage.mac.com/francines/pdfs/AdobeTypes.zip
    Unzip it. Create the ScriptingAdditions folder in the /Library folder--notice that there is no space in the name, and it is in the Library folder that you see when you open the drive, NOT the one in your home folder. Put the unzipped file in the folder.
    Francine
    Francine
    Schwieder

  • Does my Mac 10.9.5 have a built in compression system to unzip files?  If not what utility is best?

    Does my Mac 10.9.5 have a built in compression system to unzip files?  If not what utility is best?

    It does. Select any file you want to compress. CTRL- or RIGHT-click on it and select Compress ... from the context menu. To decompress simply double-click on the compressed file to open it. If you have any problems then get The Unarchiver 3.9.1 for decompressing. Use The Archive Browser 1.9.1 to do both.

  • How to unzip files, how to unzip files

    How do I unzip  files on my mba 11 inch?  I was told to use archive utilites but I can't find it.

    I am having the same problem.  I downloaded 5 Smilebox slideshows and downloaded the installer as instructed when I tried to open the files.  None open, with DiskImageMounter and Disk Utility.  Each time I get the installer instructions.

  • Unzipping files using java.util

    I am trying to unzip files using java.util.zip
    It works fine when I am trying to unzip a file that is already residing on the server.
    But I want to unzip the file that user will upload to the server .
    My code is uploading zip file to server but not able to unzip this zip file giving error
    java.util.zip.ZipException: error in opening zip file
    What could be the problem
    I am not getting exactly
    Can you give me any clue
    Thanks

    sounds like the uploading is the problem. Are you FTPing the file? If so check that it's being transferred in binary, not ascii.

  • Unzip files within labview

    I have been playing around with the new zip functions in labview 8.  I like it, nice and simple.  Then I realized something.  They don't have any functions listed to unzip files?!?!  Am I missing something, or is there another method to call zip files in labview, unzip to a location, and then grab files from the unzip within labview programatically?  The only method I can think of is to have winzip installed on the machine so you can use the command line execute function (but that is an awful method IMHO).  I would have thought that if NI got the license to build zip files they would have also gotten the rights to include the unzipping as well.

    Mads wrote:
    Unzip functions in native G would be very useful, simply because that code is supported on the RT targets. There is an unzip option in the palette in LV 8.5, but it returns error 2 if I try to unzip anything with it...if the file only contains one file it works, but it retunrs the error and with more than one file inside the archive it fails....is it supposed to work, or is there still no proper unzip support?
    I have code I want to move from a PC to a RIO unit, but the lack of a G-version of unzip makes this difficult, the OpenG alternative is not native G so no luck there...It seems the VIs available in LV 8.5 could do the job, if only some minor bug-fixing was done(?)
    ZIP/Unzip support as native VIs is not very practical. Compression is a very tricky business and developing such algorithmes in a way that always works is not very easy and quite time consuming, not to mention that LabVIEW is not the ideal invironment to do low level bit shuffling and manipulation.
    Combine that with the fact that there are ready made and VERY throughly tested open source libraries with the needed functions (of course mostly in C) and you really realize that trying to reinvint that wheel in LabVIEW itself is simply an exercise in vain.
    The OpenG ZIP/UNZIP library is based on the wide spread zlib library that just about compiles on any C compiler that dates from after the early 1960ies and so it should be no problem to grab the according C source code from the OpenG Toolkit depository on sourceforge.net and recompile it with the correct C toolchain for your CRIO realtime target.
    Oh and if you mean to transport the unzip algorithme to the FPGA target instead of the realtime target, keep dreaming. Once the FPGA chips start to reach the 1 Billion gate count this could actually start to get feasable. 3 Million gates is really nothing for such a task.
    Rolf Kalbermatter
    Message Edited by rolfk on 11-28-2007 11:22 AM
    Rolf Kalbermatter
    CIT Engineering Netherlands
    a division of Test & Measurement Solutions

  • Unzip files in Lion?

    In SL, you just clicked on the zip file and it would unzip. In Lion, that process zips the zip file. What is the correct way to unzip files in Lion?

    f0rgiven wrote:
    Where is the archive utility.app?
    It's in /System/Library/CoreServices/Archive Utility.app
    If you lauch that app, you'll have preferences available in the Menu, but I don't think it will help with your problem:

Maybe you are looking for

  • My iPhone 4 does not allow me to sync wirelessly when plugged into itunes? How do I fix this? I am running leopard on a macbook pro. Could that be the issue?

    I have updated my iphone software to ios5 and everything is working perfectly except for the wireless sync. When I plug my phone into my computer (I'm using a MacBook Pro running leopard) it does not give me the option to check the box in the summary

  • 3D nonlinear fit + smoothing

    Hello,  I am trying to fit or smooth a 3D set of data which comes from a CCD image (saved as a 32 bit bitmap ). I need to get rid of the small fringes that you can see in the bitmap attached ( hence smoothing of my data) and  find the phase of the si

  • Subtitles-.stl from dvd sp to fcp... how?

    I want to get one clip from 1 hour video. I need it with subtitles as quicktime. How can I get video with subtitle from DVDSP save as quicktime? and how can I get .stl file into FCP?

  • Server-side bitmap manipulation

    In AS3 it's possible to create new images from a bitmap source using bitmapdata and matrix. Is the same possible server-side on FMS3? Why? We want to load only the interesting portion of a big source image, instead of first loading the whole thing an

  • Using Aperture with Nikon Coolscan 5000

    I have a few thousand slides from the old days and want to pick out and save the best of them. I decided to buy a Nikon Coolscan 5000 for the job. Can Aperture be used with the Coolscan? Or, would I be better off with Silverfast? I understand that Ni