• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Compress .zip using Java

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.util.zip.ZipOutputStream
 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.exampledepot.com/egs/java.util.zip/CreateZip.html
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First put one entry, then copy its data, then put the second entry.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am passing as argument the following:

 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I am getting access through:

 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following method throws NullPointer:

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:



 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic