Java - Zipping existing files [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Appending files to a zip file with Java
Hello Java Developers,
Here's the scenario:
Say I have a textfile named sample.txt. What I actually want to do is to put the sample.txt file into a *.zip file named TextFiles.zip.
Here's what I have learned so far.
try{
File f = new File(compProperty.getZIP_OUTPUT_PATH());
zipOut = new ZipOutputStream(new FileOutputStream(f));
ZipEntry zipEntry = new ZipEntry("sample.txt");
zipOut.putNextEntry(zipEntry);
zipOut.closeEntry();
zipOut.close();
System.out.println("Done");
} catch ( Exception e ){
// My catch block
}
My code so far creates a *.zip file and insert the sample.txt file.
My question is how would I be able to insert an existing file to the created *.zip file?
If your answer has anything to do with TrueZIP, please post an SSCCE.
I have done the following:
Googled
Search for existing question. ( Found few. No answer. Some didn't answer my particular question.
Read TrueZip. Yet, I couldn't understand a thing. ( Please do understand )

Using the inbuilt Java API. This will add a file to a Zip File, this will replace any existing Zip files that may exist, creating a new Zip file.
public class TestZip02 {
public static void main(String[] args) {
try {
zip(new File("TextFiles.zip"), new File("sample.txt"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void zip(File zip, File file) throws IOException {
ZipOutputStream zos = null;
try {
String name = file.getName();
zos = new ZipOutputStream(new FileOutputStream(zip));
ZipEntry entry = new ZipEntry(name);
zos.putNextEntry(entry);
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
byte[] byteBuffer = new byte[1024];
int bytesRead = -1;
while ((bytesRead = fis.read(byteBuffer)) != -1) {
zos.write(byteBuffer, 0, bytesRead);
}
zos.flush();
} finally {
try {
fis.close();
} catch (Exception e) {
}
}
zos.closeEntry();
zos.flush();
} finally {
try {
zos.close();
} catch (Exception e) {
}
}
}
}

Here you can get answer for your question: http://truezip.schlichtherle.de/2011/07/26/appending-to-zip-files/

It seems that, according to the epic JDK reference, you could use a while zis.getNextEntry() != null loop to loop through the file (where zis is a ZipInputStream), then use zis.read() to read into an array, which is sent to an ArrayList or similar.
Then, one could use toArray(), "cast" it to a byte array with this method and zos.write() it into the output ZIP file (where zos is a ZipOutputStream), using zos.putNextEntry() to make new entries. (You will need to save the ZipEntry and get its name with ze.getName(), with ze being a ZipEntry.)You should replace T with Byte and byte (use byte everywhere but the for loop body) and may need to modify the casting code to use Byte.byteValue() to convert from Byte (wrapper class) to byte (primitive type), like so:
for(int i = 0; i < objects.length; i++) {
convertedObjects[i] = (Byte)objects[i].byteValue();
}
Note that this is untested and based on the JDK (entries ZipInputStream, ZipOutputStream, ArrayList, and Byte) and a Google search on array casting.
Sorry if that was a bit dense, and hope this helps!!

Related

Unable to read files from ZIP file input stream

I have a Zip file that I am trying to read. I do not want to use a ZipFile because in the future, I would like to do this for data that is not from a file.
This is what I have tried so far. Instead of printing the contents of res00000.dat, it prints an empty line. I do not know how to fix this
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
if (!zipEntry.getName().equals("res00000.dat")) {
zipInputStream.closeEntry();
continue;
}
}
int len;
ByteArrayOutputStream byteArrayOutputStream = new ByterrayOutputStream();
byte[] buffer = new byte[1024];
while ((len = zipInputStream.read(buffer)) > 0) {
byteArrayOutputStream.write(buffer, 0, len);
}
String xml = byteArrayOutputStream.toString();
System.out.println(xml);
zipInputStream.closeEntry();
zipInputStream.close();
return null;
My ZIP file has only two files in it. It is a Blackboard Test bank file that I'm attempting to parse:
Zip file
+-imsmanifest.xml
+-res00000.dat
Can someone please help?
You code currently doesn't handle a missing entry. It just silently scrolls to the end of a ZipInputStream so there is no way to tell what's happening. You could do following to get exception when an entry identified by name is missing:
public String readEntry(ZipInputStream in, String name) {
while ((zipEntry = in.getNextEntry()) != null) {
if (zipEntry.getName().equals(name)) {
return readXml(zipInputStream);
}
}
throw new IllegalStateException(name + " not found inside ZIP");
}
You will most likely observe above IllegalStateException now for missing res00000.dat.
Do note that there is no reason to call closeEntry() manually when scrolling the ZipInputStream as getNextEntry() already does it under the hood. From JDK 11 source code:
public ZipEntry getNextEntry() throws IOException {
ensureOpen();
if (entry != null) {
closeEntry();
}
...

producing an empty zip file java

I'm currently writing a function what would create a zip file, which will be used in other functionality. Below it is my function's code:
public void createZip(){
try{
String outfile = this.filename + ".zip";
//input file
FileInputStream input = new FileInputStream(this.filename);
//output file
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(outfile));
//name the file inside the zip file
System.out.println(this.filename);
zip.putNextEntry(new ZipEntry(this.filename));
byte[] buffer = new byte[this.BUFFER];
int len;
//copy the file to the zip
while((len= input.read(buffer)) > 0){
System.out.println(len);
zip.write(buffer, 0, len);
}
zip.closeEntry();
zip.flush();
input.close();
zip.close();
this.filename += ".zip";
}
catch(IOException e){
e.printStackTrace();
}
}
I have tried to debug, but I couldn't find the source of this problem. The function runs without any further problems, but the zip file produced it is an empty one.
You must close the entry using ZipOutputStream#closeEntry() prior to closing the output stream, or the entry is never confirmed to have been written entirely.
Also, the name of the ZipEntry cannot be the entire path; i.e, it should be dog.png instead of C:\Users\Admin\Documents\dog.png. This issue will pass by without an exception, and will cause the data from the file to be compressed directly into the zip, rather than into the zip as a compressed file.
final static byte[] EmptyZip={80,75,05,06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00};
public static void createEmptyZip(String path){
try{
FileOutputStream fos=new FileOutputStream(new File(path));
fos.write(EmptyZip, 0, 22);
fos.flush();
fos.close();
}catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
well, just wondering why you pass a filename as a parameter if you dont use it within the code..
Since you are always using the this.filename. That makes me think that you are trying to name a zip file with a name you set into the objects state and since you are also using that same name in the ZipEntry its trying to add that same zipper file inside it.. since the ZipEntry must point to an existing file, thats why it comes up empty.
Hope it helps.
Try to flush the buffer by using zip.flush() before closing, although close should flush the buffer.
Also verify this.filename. You have a local variable with the same name. The local variable filename is never used. It's possible the zip file is being written to a different location than what you expect.
#phillipe try this please
public void createZip(String filename) {
try {
//input file
FileInputStream input = new FileInputStream(filename);
//output file
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(filename + ".zip"));
//name the file inside the zip file
zip.putNextEntry(new ZipEntry(filename));
byte[] buffer = new byte[1024];
int len;
//copy the file to the zip
while((len = input.read(buffer)) > 0) {
System.out.println();
zip.write(buffer, 0 , len);
}
zip.closeEntry();
zip.flush();
zip.close();
input.close();
filename += ".zip";
} catch(IOException e) {
e.printStackTrace();
}
}
those code create a zip file and that's work and for me too. :)
Simple solution. Make one manual directory in ZipEntry without File Separator.
zip.putNextEntry(new ZipEntry("LOG" + fileName));
instead of
zip.putNextEntry(new ZipEntry(fileName));
Here fileName = file.getAbsoluteFile();
This will first create LOG dir in zip file followed by fileNames with directory path. This avoids creating initial empty directory in zip file.

How does one go about finding a specific directory out of a jar/zip file in java?

I have been working on this for quite a few hours. I can't seem to find the issue to this problem. Essentially what I have is this:
I have a jar, let's call it "a.jar"
I need to get the directory "z" and it's contents from "a.jar", but "z" isn't in the root directory of "a.jar".
"z" is in "/x/y/" and "/x/y/" is in "a.jar", so it looks like this:
"a.jar/x/y/z/"
I hope that's a decent explanation. By the way, "a.jar" is what everything is running out of, so its in the class path obviously.
Basically for each ZipEntry you have to check if it isDirectory() and parse that also.
Checkout this link:
http://www.javaworld.com/javaworld/javatips/jw-javatip49.html
LE:
Here is a complete example that extracts the files from the jar, and if you specify a specific path it will extract only that folder:
public void doUnzip(String inputZip, String destinationDirectory, String specificPath)
throws IOException {
int BUFFER = 2048;
File sourceZipFile = new File(inputZip);
File unzipDestinationDirectory = new File(destinationDirectory);
unzipDestinationDirectory.mkdir();
ZipFile zipFile;
// Open Zip file for reading
zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
// Create an enumeration of the entries in the zip file
Enumeration<?> zipFileEntries = zipFile.entries();
// Process each entry
while (zipFileEntries.hasMoreElements()) {
// grab a zip file entry
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
if(specificPath != null){
if(entry.getName().startsWith(specificPath) == false)
continue;
}
File destFile = new File(unzipDestinationDirectory, entry.getName());
// create the parent directory structure if needed
destFile.getParentFile().mkdirs();
try {
// extract file if not a directory
if (!entry.isDirectory()) {
BufferedInputStream is = new BufferedInputStream(
zipFile.getInputStream(entry));
// establish buffer for writing file
byte data[] = new byte[BUFFER];
// write the current file to disk
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos,
BUFFER);
// read and write until last byte is encountered
for (int bytesRead; (bytesRead = is.read(data, 0, BUFFER)) != -1;) {
dest.write(data, 0, bytesRead);
}
dest.flush();
dest.close();
is.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
zipFile.close();
}
public static void main(String[] args) {
Unzip unzip = new Unzip();
try {
unzip.doUnzip("test.jar", "output", "x/y/z");
} catch (IOException e) {
e.printStackTrace();
}
}
..(ZipEntry), but they don't work very well with sub-directories.
They work just fine. Iterate the entries and simply check the path equates to that sub-directory. If it does, add it to a list (or process it, whatever).

Is this an efficient way of copy files

Is this an efficient way to copy all files with in a directory, including child directories? Is there a chance of infinite recursion? Is there anything I should change? I know it works, but I think there should be an easier way to do this.
private void copy(File file, String path) {
String fileName = file.getPath();
System.out.println(fileName);
fileName = fileName.substring(fileName.lastIndexOf("\\"));
if (path == null)
path = Storage.getStorageDirectoryPath();
File toWrite = new File(path + File.separator + fileName);
if (file.isDirectory()) {
toWrite.mkdir();
File inDirectory[] = file.listFiles();
for (File f : inDirectory)
copy(f, toWrite.getPath());
} else {
try {
InputStream inStream = new FileInputStream(file);
OutputStream outStream = new FileOutputStream(toWrite);
byte buffer[] = new byte[1024];
int length = 0;
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Thanks
Looks pretty good as the comments indicate. You might want to look into the new Java 7 API's (the new NIO). There's a tutorial here, it looks like there are even options to avoid following links.
If you can't use Java 7, old NIO has channels that you can open after opening the file the old way. They include methods transferFrom and transferTo, which might be able to do it more efficiently than you could in Java.
Why reinvent the wheel? take a look at the methods in Apache Common's FileUtils, in particular copyDirectory.

Java - copy Jar Folder

Actually I am confronted with a Problem. I've got a ".apk-File" in one Package of my Application. apk is a kind of a jar File (apk = Android Package).
I now want to copy this jar-file out of my Programm onto any other Location at the PC.
Normally I would do this by using:
FileInputStream is = new FileInputStream(this.getClass().getResource("/resources/myApp.apk").getFile());
And then write it on the disk with using a FileOutputStream.
... but since an .apk is a kind of a .jar it doesn't work. It just copies the .apk file. but without the containing other files.
any help would be appreciated
Since .apk is a .jar file by another name (in other words it is a zip file with some specific definitions of where configuration files are stored inside the directory) then look at ZipInputStream to read the file and walk through the contents and write them out as files.
Thank you very much Yishai... this was the Hint I was waiting for :)
Probably is sb out there, who needs the to do a same thing, therefore... here is my code:
public static boolean copyApkFile(File outputFile){
try {
FileInputStream fis = new FileInputStream(this.getClass().getResource("/resources/myApkFile.apk").getFile());
ZipInputStream zis = new ZipInputStream(fis);
FileOutputStream fos = new FileOutputStream(outputFile));
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry ze = null;
byte[] buf = new byte[1024];
while ((ze = zis.getNextEntry()) != null) {
System.out.println("Next entry "+ze.getName()+" "+ze.getSize());
zos.putNextEntry(ze);
int len;
while ((len = zis.read(buf)) > 0) {
zos.write(buf, 0, len);
}
}
zos.close();
fos.close();
zis.close();
fis.close();
return true;
} catch (IOException ex) {
Logger.getLogger(SetUpNewDevice.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}

Categories

Resources