aspose file tools*
The moose likes Java in General and the fly likes Compress .zip using Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Compress .zip using Java" Watch "Compress .zip using Java" New topic
Author

Compress .zip using Java

André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234


Hey guys, how are you?
I am just looking for a method that could compress more than one file passed as argument in only one .zip, for exemple I have two files and one folder after compressing will be only one file named compressedallfile.zip. Got it?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

java.util.zip.ZipOutputStream

Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
Hi James, thanks for helping me... I already know that Java's class however I can't use it to multi-files I only know to use it for only one file, look at below my code please:




A detail the last one file override the first one... got it?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42945
    
  68
http://www.exampledepot.com/egs/java.util.zip/CreateZip.html
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

First put one entry, then copy its data, then put the second entry.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Moving discussion as too difficult for "beginning Java™"
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
First of all, thanks all for helping me...


Ulf Dittmer wrote:http://www.exampledepot.com/egs/java.util.zip/CreateZip.html


Hey Ulf, thanks a lot for sending this link, that really works, but for my case I need also delete folders... And during deleting I face that exception:



Look the method I have now:

André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
That works perfectly to files when try using with folders don't find the folder's directory and says I don't have permissions to have access through.
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
I am passing as argument the following:

James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

André Asantos wrote:I am passing as argument the following:


I don't know about "mimetype" and "OEBPS" but "META-INF" is normally a directory. You need to pass a list of all the files you want to include in the zip file; not just a list of directories.

Using recursion it is almost trivial to traverse a directory and add to the zip file the files one finds. Just make sure you don't try to zip the zip file into itself!
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
hmmm excelent tip...

I have got to do following below now:

That works:







The only problem is generate exception saying:



I am worried about the exception... Could it be not important to worry?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Never having seen that exception I'm on uncertain ground here. Though I don't like your use of 'continue' there is nothing that stands out to me as being wrong. Points :-

1) You would do better to use File#listFiles() rather than File#list() since this would return you an array of files. It is usually better to work with File objects than String objects when dealing with files. Note that the return list can be 'null' so make sure you handle that.
2) Having never used it in this scenario I'm not sure what happens creating the entry name of f.getPath(). I always use f.getCanonicalPath() and strip off the first N characters when N is the length of the root.getCanonicalPath(). Example -

3) I seem to remember that the path separator in zip files is always '/' but I can't be sure.
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
Hey good morning sir.... Could you please send me an exemple?
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234


I am getting access through:

André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
The problem is on my code structure to compress.
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
The following method throws NullPointer:

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42945
    
  68
Were you planning on telling us in which line the NPE happens, and which object is null (which I assume you have investigated before posting here)?
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
Ulf Dittmer, you are right, let me base on below source before posting more.

I will be right back... Bye


http://download.oracle.com/javase/6/docs/api/
and
http://java.sun.com/developer/technicalArticles/Programming/compression/
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
Sir, I tried all day to fix this problem but I can't... My doubt is howto transform/do this following method that works compress a directory/folder
as well because currently only compress files... I know that I need recursivity but I don't get to do sorry... Look at below please:



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compress .zip using Java