-
Please urgent how to zip multiple files and directory to one output
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
import java.util.zip.*;
import java.io.*;
public class zip_files
{
public static void main(String[] args) throws IOException
{
File[] all = {new File("C:\\cdrecord.exe"), new File("C:\\mkisofs.exe")};
createZip(new File("c:\\output.zip"),all);
}
public static void createZip(File zipFileName,File[] selected)
{
try
{
byte[] buffer = new byte[1024];
ZipOutputStream out = new ZipOutputStream(new
FileOutputStream(zipFileName));
out.setLevel(Deflater.DEFAULT_COMPRESSION);
for (int i = 0; i < selected.length; i++)
{
FileInputStream in = new FileInputStream(selected[i]);
out.putNextEntry(new ZipEntry(selected[i].getPath()));
int len;
while ((len = in.read(buffer)) > 0)
{
out.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();
}
}
}
Zip only a directory
import java.util.zip.*;
import java.io.*;
public class zip_dir_all
{
public static void main(String[] args) throws IOException
{
String dirpath = "c:\\jtutor";
String ZipName = "c:\\output.zip";
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(ZipName));
zipdirectory(dirpath,zos);
zos.close();
}
public static void zipdirectory(String dirpath, ZipOutputStream zos) throws IOException
{
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();
}
}
}
Zip only a sub directory
import java.util.zip.*;
import java.io.*;
public class zip_dir
{
public static void main(String[] args) throws IOException
{
File[] alle = {new File("c:\\ftpjakarta")};
String dirpath = "c:\\jtutor";
String ZipName = "c:\\output.zip";
zipdirectory(dirpath,ZipName);
}
public static void zipdirectory(String dirpath, String ZipName) throws IOException
{
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(ZipName));
File f = new File(dirpath);
String[] flist = f.list();
int buffersize = 1024;
byte[] buffer = new byte[buffersize];
int count;
for(int i=0; i<flist.length; i++)
{
File ff = new File(f,flist[i]);
if(ff.isDirectory())
continue;
String filepath = ff.getPath();
ZipEntry entries = new ZipEntry(filepath);
zos.putNextEntry(entries);
FileInputStream fis = new FileInputStream(ff);
while((count = fis.read(buffer)) != -1)
{
zos.write(buffer,0,count);
}
fis.close();
}
zos.close();
}
}
Any help will be appreciated. Hier is my email address : brusli@excite.com
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks