Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Not able to put some files inside Zip file  RSS feed

 
Chetan Ram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have some files(.xml files) to be placed inside a zip file. I tried below code, where instead of placing (.xml)files inside the zip files, its removing all the existing files from zip file and not inserting the (.xml) files. Please let me know if i am missing something

try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream("c:\\zip\\myfigs.zip");
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
dest));
out.setMethod(ZipOutputStream.DEFLATED);
byte data[] = new byte[BUFFER];
// get a list of files from current directory
// File f = new File(".");
File f = new File("C:\\outputFile");
String files[] = f.list();

for (int i = 0; i < files.length; i++) {
System.out.println("Adding: " + files[i]);
FileInputStream fi = new FileInputStream(f.getAbsoluteFile()
+ "\\" + files[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(f.getAbsoluteFile() + "\\"
+ files[i]);
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}

Thanks,
Chinnu
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct, the java.util.zip package can not append to existing files, it can only create new files. You need to unzip the existing file, and then zip all old and new files together into a new ZIP file.
 
Chetan Ram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dittmer, I would like to clarify what i have understood from your reply.

1. I have 123.xml, 234.xml and test.txt files in a ZipTest directory (c:\\ZipTest) folder.

2. At runtime, i need to create a zip file from my java program (c:\\testNewZip.zip)

3. I have to code in such a way that it should put all the files from Zip directory to a new created zip file (c:\\testNewZip.zip).

Please let me know if my understanding is correct or not.

Thanks,
Chinnu

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's correct, although I'm not sure why you're asking this - isn't that exactly what the code you posted does?
 
Chetan Ram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dittmer for your reply.

from the above code, its creating a zip file but, not placing the files inside it.

i.e. FileOutputStream dest = new FileOutputStream("c:\\myfigs.zip"); // does not exists. So creating a zip file (myfigs.zip)

but,

File f = new File("C:\\outputFile");
FileInputStream fi = new FileInputStream(f.getAbsoluteFile()
+ "\\" + files[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(f.getAbsoluteFile() + "\\"
+ files[i]);
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}

Its not adding files (*.xml and .txt) which are inside ("C:\\outputFile") into the zip file created above (myfigs.zip).

When i open myfigs.zip, its empty and am not getting any exception.

Please advice.

Thanks,
Chetan.
 
Chetan Ram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one please let me know how to zip a directory which contains .xml and .txt files with sub directories?

I tried below code but, its placing all the .xml, .txt files from the directory and sub directory and the sub-directory itself.

for ex: zip1 is a directory {123.xml, 234.xml and zip234 as sub-directory}

Now, i want to zip this files and sub-directories i.e. ZipTest.zip file should contain 123.xml, 234.xml and zip234, zip345 as sub-directory.

And zip234 sub-directory can contain some more sub-directory.


public static void main(String[] args) {
String zipFileName = "C:\\compressed3.zip";
ZipOutputStream zout;
try {
zout = new ZipOutputStream(new FileOutputStream(zipFileName));
createZipFile("C:\\testZip", zout);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


private static void createZipFile(String string, ZipOutputStream zout) {
FileInputStream fis = null;
try {
/*ZipFile zipFile = new ZipFile(string);
ZipEntry entry = zipFile.getEntry(string);*/
File die = new File(string);
File[] files = die.listFiles();

for (int i =0; i<files.length;i++){

if(files[i].isDirectory())
{
//if the File object is a directory, call this
//function again to add its content recursively
//String filePath = f.getPath();
String filePath = files[i].getPath();
createZipFile(filePath, zout);
//loop again
continue;
}
fis = new FileInputStream(files[i]);
zout.putNextEntry(new ZipEntry(files[i].getName()));

byte[] buffer = new byte[1024];
int bytesRead;

//Each chunk of data read from the input stream
//is written to the output stream
while ((bytesRead = fis.read(buffer)) > 0) {
zout.write(buffer, 0, bytesRead);
}

//Close zip entry and file streams
zout.closeEntry();
}

} catch (IOException e) {
e.printStackTrace();
} finally{
try {
zout.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Please Advice.

Thanks,
Chinnu
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!