• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compress .zip using Java

 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.zip.ZipOutputStream
 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First put one entry, then copy its data, then put the second entry.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving discussion as too difficult for "beginning Java™"
 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am passing as argument the following:

 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey good morning sir.... Could you please send me an exemple?
 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am getting access through:

 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is on my code structure to compress.
 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following method throws NullPointer:

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic