Hi,
As a part of my project, i need to compress folders and files into a single zip file as follows.
for eg:
I want to compress
C:\test\
c:\compression\
C:\documents\te st.txt
all of these into a single zip file... I need to maintain the folder structure as well, when i unzip them using WINZIP.
I am able to compress each of them seperately, but not together...I am making use of java.util.zip.* ;
Is there anyway to append more files to an existing zip file??? i am a bit confused with the input and output streams now!!!
Urgent help required.
Thanks in advance..
My code is as follows: [I am open to all suggestions!!!]
[code=java]
import java.io.*;
import java.util.zip.* ;
public class ZipCreateExampl e {
static ZipOutputStream cpZipOutputStre am = null;
String[] strSource=new String[3];
static String strTarget = "";
String strSubstring = "";
public static void main(String[] args) throws IOException
{
System.out.prin tln("Example of ZIP file creation.");
// Specify files to be zipped
String[] filesToZip = new String[3];
filesToZip[0] = "C:\\compress.j ava";
filesToZip[1] = "C:\\test.t xt";
filesToZip[2] = "C:\\Compresste st";
int n=filesToZip.le ngth;
// Specify zip file name
String zipFileName = "c:\\example2.z ip";
ZipCreateExampl e udZipUtility = new ZipCreateExampl e();
for(int i=0;i<n;i++)
udZipUtility.st rSource[i] = filesToZip[i];
udZipUtility.st rTarget = zipFileName;
udZipUtility.zi p(filesToZip,n) ;
cpZipOutputStre am.close();
System.out.prin tln("\n Finished creating zip file " + strTarget + " from source ");
}
private void zip(String[] filesToZip, int n){
try
{ ///int count=0;
for(int i=0;i<n;i++)
{
File cpFile = new File(filesToZip[i]);
System.out.prin tln(filesToZip[i]);
if (!cpFile.isFile () && !cpFile.isDirec tory() ) {
System.out.prin tln("\nSource file/directory Not Found!");
return;
}
if (cpFile.isDirec tory()) {
strSubstring = strSource[i];
} else {
strSubstring = "";
}
String zipFileName = "c:\\example2.z ip";
//count=count+1;
cpZipOutputStre am = new ZipOutputStream (new FileOutputStrea m(zipFileName)) ;
cpZipOutputStre am.setLevel(9);
zipFiles(cpFile );
//cpZipOutputStre am.finish();
//cpZipOutputStre am.close();
//System.out.prin tln("\n Finished creating zip file " + strTarget + " from source " + strSource);
}
//cpZipOutputStre am.finish();
cpZipOutputStre am.close();
//System.out.prin tln("\n Finished creating zip file " + strTarget + " from source ");
}
catch (Exception e){
e.printStackTra ce();
}
}
private void zipFiles(File cpFile) {
if (cpFile.isDirec tory()) {
File [] fList = cpFile.listFile s() ;
for (int i=0; i< fList.length; i++){
zipFiles(fList[i]) ;
}
} else {
try {
String strAbsPath = cpFile.getAbsol utePath();
String strZipEntryName ="";
if (!strSubstring. equals("") ){
strZipEntryName = strAbsPath.subs tring(strSource .length+1, strAbsPath.leng th());
} else {
strZipEntryName = cpFile.getName( );
}
byte[] b = new byte[ (int)(cpFile.le ngth()) ];
FileInputStream cpFileInputStre am = new FileInputStream (cpFile) ;
ZipEntry cpZipEntry = new ZipEntry(strZip EntryName);
// Add ZIP entry to output stream.
cpZipOutputStre am.putNextEntry (cpZipEntry );
//int i = cpFileInputStre am.read(b, 0, (int) cpFile.length() );
cpZipOutputStre am.write(buffer ,0,cpFileInputS tream.read(buff er));
/*
int len;
while ((len = cpFileInputStre am.read(b)) > 0)
{
cpZipOutputStre am.write(b,0,le n);
}
cpZipOutputStre am.closeEntry() ;
//cpZipOutputStre am.putNextEntry (cpZipEntry );
//cpZipOutputStre am.write(b, 0, (int)cpFile.len gth());
//cpFileInputStre am.close();
*/
} catch (Exception e) {
e.printStackTra ce();
}
}
}
}[/code]
As a part of my project, i need to compress folders and files into a single zip file as follows.
for eg:
I want to compress
C:\test\
c:\compression\
C:\documents\te st.txt
all of these into a single zip file... I need to maintain the folder structure as well, when i unzip them using WINZIP.
I am able to compress each of them seperately, but not together...I am making use of java.util.zip.* ;
Is there anyway to append more files to an existing zip file??? i am a bit confused with the input and output streams now!!!
Urgent help required.
Thanks in advance..
My code is as follows: [I am open to all suggestions!!!]
[code=java]
import java.io.*;
import java.util.zip.* ;
public class ZipCreateExampl e {
static ZipOutputStream cpZipOutputStre am = null;
String[] strSource=new String[3];
static String strTarget = "";
String strSubstring = "";
public static void main(String[] args) throws IOException
{
System.out.prin tln("Example of ZIP file creation.");
// Specify files to be zipped
String[] filesToZip = new String[3];
filesToZip[0] = "C:\\compress.j ava";
filesToZip[1] = "C:\\test.t xt";
filesToZip[2] = "C:\\Compresste st";
int n=filesToZip.le ngth;
// Specify zip file name
String zipFileName = "c:\\example2.z ip";
ZipCreateExampl e udZipUtility = new ZipCreateExampl e();
for(int i=0;i<n;i++)
udZipUtility.st rSource[i] = filesToZip[i];
udZipUtility.st rTarget = zipFileName;
udZipUtility.zi p(filesToZip,n) ;
cpZipOutputStre am.close();
System.out.prin tln("\n Finished creating zip file " + strTarget + " from source ");
}
private void zip(String[] filesToZip, int n){
try
{ ///int count=0;
for(int i=0;i<n;i++)
{
File cpFile = new File(filesToZip[i]);
System.out.prin tln(filesToZip[i]);
if (!cpFile.isFile () && !cpFile.isDirec tory() ) {
System.out.prin tln("\nSource file/directory Not Found!");
return;
}
if (cpFile.isDirec tory()) {
strSubstring = strSource[i];
} else {
strSubstring = "";
}
String zipFileName = "c:\\example2.z ip";
//count=count+1;
cpZipOutputStre am = new ZipOutputStream (new FileOutputStrea m(zipFileName)) ;
cpZipOutputStre am.setLevel(9);
zipFiles(cpFile );
//cpZipOutputStre am.finish();
//cpZipOutputStre am.close();
//System.out.prin tln("\n Finished creating zip file " + strTarget + " from source " + strSource);
}
//cpZipOutputStre am.finish();
cpZipOutputStre am.close();
//System.out.prin tln("\n Finished creating zip file " + strTarget + " from source ");
}
catch (Exception e){
e.printStackTra ce();
}
}
private void zipFiles(File cpFile) {
if (cpFile.isDirec tory()) {
File [] fList = cpFile.listFile s() ;
for (int i=0; i< fList.length; i++){
zipFiles(fList[i]) ;
}
} else {
try {
String strAbsPath = cpFile.getAbsol utePath();
String strZipEntryName ="";
if (!strSubstring. equals("") ){
strZipEntryName = strAbsPath.subs tring(strSource .length+1, strAbsPath.leng th());
} else {
strZipEntryName = cpFile.getName( );
}
byte[] b = new byte[ (int)(cpFile.le ngth()) ];
FileInputStream cpFileInputStre am = new FileInputStream (cpFile) ;
ZipEntry cpZipEntry = new ZipEntry(strZip EntryName);
// Add ZIP entry to output stream.
cpZipOutputStre am.putNextEntry (cpZipEntry );
//int i = cpFileInputStre am.read(b, 0, (int) cpFile.length() );
cpZipOutputStre am.write(buffer ,0,cpFileInputS tream.read(buff er));
/*
int len;
while ((len = cpFileInputStre am.read(b)) > 0)
{
cpZipOutputStre am.write(b,0,le n);
}
cpZipOutputStre am.closeEntry() ;
//cpZipOutputStre am.putNextEntry (cpZipEntry );
//cpZipOutputStre am.write(b, 0, (int)cpFile.len gth());
//cpFileInputStre am.close();
*/
} catch (Exception e) {
e.printStackTra ce();
}
}
}
}[/code]
Comment