aspose file tools*
The moose likes Java in General and the fly likes how to zip multiple files and directory into one output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to zip multiple files and directory into one output" Watch "how to zip multiple files and directory into one output" New topic
Author

how to zip multiple files and directory into one output

benny rusli
Ranch Hand

Joined: Jan 15, 2005
Posts: 72
I have a problem, how to zip multiple files and directory to one output. As example i want to zip "c:\\Programme", "c:\\excel.xls","c:\\WINNT","c:\\test.txt" that means c:\\Programme+c:\\excel.xls+c:\\WINNT+c:\\test.txt = output.zip or i can say directory+directory+files+files = output.zip. I can only until now zip either only files or directory, hier ist my code.

Zip only a files



Zip only a directory



Zip only a sub directory




Any help will be appreciated.

[added code tags and removed urgent from subject line and email address - Ilja]
[ January 17, 2005: Message edited by: Ilja Preuss ]
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

You have code to place files in ZipOutputStream, and code to put the contents of a directory into a ZipOutPutStream. All you need to do is call the different code on the same ZipOutputStream. Some of your methods are opening and then closing the ZipOutputStream. You can change these methods so instead of taking the zip file name as a parameter & creating (and then closing) an ZipOutputStream, they take an already created/open ZipOutputStream as a parameter. You did this for the zipdirectory method in the zip_dir_all class. Then just call each method on the same stream, then close your stream. Thus your main would look something like this:


I probably would change the name of the createZip method to something like zipFiles so it makes more sense, especially when used along side the zipDirectory method.

Also, as a side note, be careful with the names you choose for your variables. In the zipdirectory method of the zip_dir_all class you have a File object (representing the directory) named f, and one (represent an individual file within the directory) named ff. This makes the code confusing and hard to read. Good choices in variable names make code self commenting, and easier to read. Names of directory and singleFile (or even file) would make that method much easier to read.

Secondly, as a convention, class names should be capitalized, so your class names should be something like Zip_files rather then zip_files, or better yet, ZipFiles.

And as one last comment, when posting code, please use UBB code tags [code] [/code] It will preserve indentation and spacing of code, making it much easier to read. More information on how to use UBB tags is available here. Some forum members will not take the time to read through code that is not contained in [code] tags since it is so much harder to read; so you will be helping yourself, and get faster responses, by using the tags.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Hello arifin,

a hearty welcome at the Ranch!

Besides using code tags as Mark explained, please also don't press us and use the forum, not email.

Thank you very much and have fun at the Ranch!


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
benny rusli
Ranch Hand

Joined: Jan 15, 2005
Posts: 72
thanks for your answering, but i cannot get it in my mind, can you give me an example more complete thanks
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Since this sounds like a homework problem, I don't want to just give the answer. Even if it isn't a homework problem, the best way to learn is to figure things out; that way it will stick with you.

From the sound of it, I think you are not understanding the core concepts of an OutputStream, since a ZipOutputStream is simply an extension (a subclass technically) of an OutputStream. I can give you a quick overview and some further explanation, but I think it will help for you to try and better understand the necessary IO fundamentals.

Once you open the ZipOutputStream, you can continue to add "items" to it until you close it. So you can open the stream, add some files, add a directory, add more files, do other things to it, etc. until the stream is closed:


This is exactly the same as if I opened a FileOutputStream to write a log file. (Both ZipOutputStream and FileOutputStream are concrete subclasses of the abstract class OutputStream). When I open a log file (using a FileOutputStream) I can write to that log file multiple times until I close it. I can do other functions between writes to the log file. As long as I do not close the FileOutputStream, and maintain a reference to it, I can continue to write to my log file. The same thing holds for a ZipOutputStream.

Take a look at the Sun Tutorial on I/O: Reading and Writing. While this does not discuss ZipOutputStreams specifically, the fundamental concepts of using an OutputStream is the discussed and is the same core concept to using a ZipOutputStream. I would also recommend looking at the two chapters (17 & 18) on IO in Just Java Sixth Edition (or even the fifth edition) by Peter van der Linden. I have found it to be one of the better introductory treatises on IO in Java. Stop by a book store and read through those chapters. After doing so, you'll likely want to purchase the book; but more importantly you will have a much better handle (no pun intended) on IO.

Below is a little more code to help point you in the right direction. Again, I will emphasis that if you "just figure out" the syntax you need to do this particular operation without getting the a solid understanding of the underlying fundamentals, you will always struggle when needing to do IO. And getting that core understanding will make future coding easier (and rather fun actually).



I hope that helps. If not, let us know and we'll see what else we can do to help you along.
[ January 17, 2005: Message edited by: Mark Vedder ]
benny rusli
Ranch Hand

Joined: Jan 15, 2005
Posts: 72
Hallo Mark,
you are worthy to be honored, it got working, you are a genius man. The next problem is, I want to zip again the same file and directory but not all only the change or the new one should be zipped and with the same name "File zipFileName = new File("C:\\myArchive.zip")", that means adding new files or directory if there is one, or change the alt files or directory with the newest one. How can I do that, should I compare each element(or entry) from within the myArchive.zip with the files and directory. That means I should first extract all the myArchive.zip into temporary directory and compare it, but the problem if the myArchive.zip about 20 GB, the extracting will take much longer(maybe 10 hours).
The next one, how can I limit my zip into 700 MB, so I can later burn it to cd rohling, is that possible to do that.
Do you have opinion or idea ? you are the master of java, i am only the beginner.


Here is the code that got working
import java.io.*;
import java.util.zip.*;

public class zipall
{
public static void main(String[] args) throws IOException
{
File[] files = {new File("C:\\cdrecord.exe"), new File("C:\\mkisofs.exe")};
String dirPath = "C:\\jtutor";
File zipFileName = new File("C:\\myArchive.zip");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
zipFiles(files, out);
zipDirectory(dirPath, out);
out.close();
}

public static void zipFiles(File[] files, ZipOutputStream zos)
{
try
{
byte[] buffer = new byte[1024];
//ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
//out.setLevel(Deflater.DEFAULT_COMPRESSION);
for (int i = 0; i < files.length; i++)
{
FileInputStream in = new FileInputStream(files[i]);
zos.putNextEntry(new ZipEntry(files[i].getPath()));
int len;
while ((len = in.read(buffer)) > 0)
{
zos.write(buffer, 0, len);
}
//out.closeEntry();
in.close();
}
//out.close();
}
catch (IllegalArgumentException iae)
{
iae.printStackTrace();
}
catch (FileNotFoundException fnfe)
{
fnfe.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}


public static void zipDirectory(String dirPath, ZipOutputStream zos) throws IOException
{ /* Place appropriate code form your "zipdirectory" method here */
File f = new File(dirPath);
String[] flist = f.list();
for(int i=0; i<flist.length; i++)
{
File ff = new File(f,flist[i]);
if(ff.isDirectory())
{
zipDirectory(ff.getPath(),zos);
continue;
}
String filepath = ff.getPath();
ZipEntry entries = new ZipEntry(filepath);
zos.putNextEntry(entries);
FileInputStream fis = new FileInputStream(ff);
int buffersize = 1024;
byte[] buffer = new byte[buffersize];
int count;
while((count = fis.read(buffer)) != -1)
{
zos.write(buffer,0,count);
}
fis.close();
}
}
}

someone should not all the life throw away, because there is a little damage in the life.
benny rusli
Ranch Hand

Joined: Jan 15, 2005
Posts: 72
sorry mark I forgot to use code tags for my code,
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Originally posted by arifin rusli:
sorry mark I forgot to use code tags for my code


You can edit a post (one of your one posts that is) after its initial submission. Just click the edit icon (the paper with the pencil) at the top of the particular post you want to edit (again, it must be one of your own posts as only moderators (bartenders & sheriffs) can edit other people's posts). You can correct typos or add forgotten tags in on the edit page.

As for your question about zipping files again and only incorporating the changed files, that gets to be a little involved. Too involved a subject I am afraid to easily be answered in a forum post. I recommend looking at the O'Reilly book Java I/O By Elliotte Rusty Harold (info at Publisher's Site). It does a great job at presenting I/O concepts, and has a chapter on Compressing Streams (such as the ZipOutputStream and ZipInputStream). However, you will likely need to read most of the chapters to get all the understanding necessary to perform this type of operation. You can also take a look at the JavaWorld.com article Modify archives, Part 1 and Part 2. It discusses concepts along the line of what you are looking to do.

The same thing applies to your question on limiting your zip file size to 700MB, and spanning an archive across multiple zip files, all limited to 700MB. It can certainly be done, but again, is not a beginner level subject and really cannot be explained in a couple of paragraphs on a forum. Again, I would recommend O'Reilly's Java I/O and any other subject matter on I/O. There may be some good articles at JavaWorld.com and other sites the help point you in the right direction. Nonetheless, it is something you will need to invest some time into learning and doing. But it will be time well spent.
benny rusli
Ranch Hand

Joined: Jan 15, 2005
Posts: 72
Hallo mark,
i have read the book that you suggested to me, that is good for beginner, but that did not answer my problem and the question that i have asked you is important because that is for my dissertation. I beg your help, can you guide me throught that, what should I do first. I know only, I must extract the old.zip to temporary directory and then make a comparing for the files, but the problem if i do that, that will wasting time, can you imagine if the file 50 GB, how long will it takes. I need someone help, please.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
There you go again pressing us.

If this is for your dissertation maybe you can spend some more time to figure stuff out on your own. (did I just say that?)
[ January 27, 2005: Message edited by: Carol Enderlin ]
 
 
subject: how to zip multiple files and directory into one output