Question:
Set zip file directory structure through Java?
Jon T
2009-08-31 11:37:28 UTC
I am creating a zip file that is supposed to contain a single xml file in the root directory. The xml is stored in a location separate from the program. When I pass the file name to the java ZipEntry constructor and add it to the ZipOutputStream it ends up creating the file location as the directory structure within the zip file. (in my code example, my xml file is inside a folder called "xmlFiles")

How do I remove the structure when creating the ZipEntry when all it asks for is a String?

<< code >>

// These are the files to include in the ZIP file
String[] filenames = new String[]{"xmlFiles\myXmlFile.xml"};

// Create a buffer for reading the files
byte[] buf = new byte[1024];

try {
// Create the ZIP file
String outFilename = "myZipFileName.zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

out.setLevel(0);
// Compress the files
for (int i=0; i FileInputStream in = new FileInputStream(filenames[i]);

// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(filenames[i]));

// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}

// Complete the entry
out.closeEntry();
in.close();
}

// Complete the ZIP file
out.close();
} catch (IOException e) {
}
Three answers:
?
2009-08-31 11:58:56 UTC
To keep your code as is just make this changes



I just added

String[] filenamesAtRoot = new String[]{"myXmlFile.xml"};



And changed this Line

out.putNextEntry(new ZipEntry(filenamesAtRoot [i]));





The explanation is easy.

The constructor of the Zip Entry especifies the Path in the ZipFile.

(or the reference to the File)

----

out.putNextEntry(new ZipEntry(filenames[i]));

----



And when you are writing the to the Zip

out.write(buf, 0, len);



what you are writing it is just the Content of the File

FileInputStream in = new FileInputStream(filenames[i]);





















<< code >>



// These are the files to include in the ZIP file

String[] filenames = new String[]{"xmlFiles\myXmlFile.xml"};

String[] filenamesAtRoot = new String[]{"myXmlFile.xml"};





// Create a buffer for reading the files

byte[] buf = new byte[1024];



try {

// Create the ZIP file

String outFilename = "myZipFileName.zip";

ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));



out.setLevel(0);

// Compress the files

for (int i=0; i
FileInputStream in = new FileInputStream(filenames[i]);



// Add ZIP entry to output stream.

out.putNextEntry(new ZipEntry(filenamesAtRoot [i]));



// Transfer bytes from the file to the ZIP file

int len;

while ((len = in.read(buf)) > 0) {

out.write(buf, 0, len);

}



// Complete the entry

out.closeEntry();

in.close();

}



// Complete the ZIP file

out.close();

} catch (IOException e) {

}
anonymous
2016-11-14 01:07:50 UTC
Java Create Zip File
selders
2016-12-24 19:40:12 UTC
Are you attempting to apply programs from the command-line? it quite is meant to be academic to apply the command-line, yet as quickly as you have the basics down, i could use an IDE like Eclipse. as quickly as you have Eclipse set up, making an investment it slow, it quite is sooo lots greater powerful at coping with assorted programs, and providing you with warnings that help you to sidestep bugs and lots else. I fantastically advise Eclipse for extreme Java programming.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...