Java not writing to zip file - java

I have the following java method which I'm trying to use to create a zip file, which has an existing file written into it (the zip file has the same name, just with the .log extension replaced with .zip) . The zip file is created successfully, however the file is not inside it when it completes.
Here is my code:
private static void zipFile(File fileToZip) {
final int bufferSize = 2048;
File zipFile = new File(fileToZip.getAbsolutePath().replaceAll(".log", ".zip"));
try (FileOutputStream fos = new FileOutputStream(zipFile.getAbsolutePath());
FileInputStream fis = new FileInputStream(fileToZip);
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
BufferedInputStream origin = new BufferedInputStream(fis, bufferSize)) {
ZipEntry ze = new ZipEntry(fileToZip.getAbsolutePath());
zos.putNextEntry(ze);
byte[] data = new byte[bufferSize];
int count;
while ((count = origin.read(data, 0, bufferSize)) != -1) {
LOGGER.info("WRITING!!!");
zos.write(data, 0, count);
}
zos.closeEntry();
} catch (IOException e) {
LOGGER.error("Error: ", e);
}
}
Any ideas? :)

Change
ZipEntry ze = new ZipEntry(fileToZip.getAbsolutePath());
to
ZipEntry ze = new ZipEntry(fileToZip.getName());

Related

Java file zip is creating corrupt files

I am attempting to zip files from one folder into a zip folder located in another directory. The problem is that the zip folder is becoming corrupted. What is causing this and how do I fix it.
FileInputStream fis = null;
FileOutputStream fos = new FileOutputStream("C:/Users/admin/Documents/files.zip");
ZipOutputStream zos = new ZipOutputStream(fos);
File folder = new File("C:/Users/admin/Documents/filestozip");
for(File file:folder.listFiles())
{
fis = new FileInputStream(file);
ZipEntry entry = new ZipEntry(file.getAbsolutePath());
zos.putNextEntry(entry);
byte[] bytes = new byte[1024];
int length;
while((length = fis.read(bytes)) >= 0)
{
zos.write(bytes, 0, length);
}
zos.closeEntry();
fis.close();
}
zos.flush();
zos.finish();
zos.close();
fos.flush();
fos.close();

Folder are file not zipping

I have implemented Zip a folder or file then download and saving it to memory.
My problem is, it is downloading without any error but i am not getting Zip file, and if i click on downloaded file it is showing:
Can't show
Or
Can't display message.
My code:
String fileName = tvtitle.getText().toString();
String fileExtension = tvtype.getText().toString();
File imageDirectory = new File(Path);
imageDirectory.mkdirs();
int fileLength = connection.getContentLength();
String _path = Path;
input = connection.getInputStream();
File outputFile = new File(_path, fileName + fileExtension);
FileOutputStream fos = new FileOutputStream(outputFile);
ZipOutputStream zos = new ZipOutputStream(fos);
File srcFile = new File(input.toString());
byte[] buffer = new byte[1024];
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int length;
while ((length = input.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
input.close();
zos.close();
I just don't know what is the type of this variable "input"
I think you should have FileInputStream (reading source file) to be paired with FileOutputStream (set to destination/zip file).
And this line of code:
File outputFile = new File(_path, fileName + fileExtension);
Your output must be .zip file right?
so, it should be:
File outputFile = new File(_path, fileName + ".zip");
or something similar
And it will be like this
String fileName = tvtitle.getText().toString();
String fileExtension = tvtype.getText().toString();
File imageDirectory = new File(Path);
FileInputStream fis = new FileInputStream(imageDirectory);
ZipEntry zipEntry = new ZipEntry(fileName);
FileOutputStream fos = new FileOutputStream("test.zip");
ZipOutputStream zos = new ZipOutputStream(fos);
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
fis.close();
zos.close();
fos.close();

Add files to a folder inside a zip that is inside a zip in Java

I have a zip file that contains a zip that includes folders to which I have to add some files. The structure is like
outerZip.zip
|folder
|innerZip.zip
|innerFolder
I need to add a few files to the inner folder.
I can return the innerZip ok, if I save it to file it shows the correct structure.
Now when I want to iterate through the ZipEntries of the innerZip for the first entry it shows to be /innerFolder, but when I want to add the entry content to the updated zip file it turns out that it contains data and throws the below error:
java.util.zip.ZipException: invalid entry CRC (expected 0x0 but got 0x46480bab)
at java.util.zip.ZipInputStream.read(ZipInputStream.java:218)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at zip.builder.InnerBuilder.addFilesToInnerZip(InnerBuilder.java:179)
at zip.builder.InnerBuilder.addFilesToZip(InnerBuilder.java:117)
The code is here:
private byte[] addFilesToZip(byte[] outerZipBytes, Set<File> files) {
String zipPath = System.getProperty("catalina.home") + File.separator + "outerZipFile.zip";
File zipFile = new File(zipPath);
ZipFile outerZipFile = null;
try {
zipFile.createNewFile();
FileOutputStream fileout = new FileOutputStream(zipFile);
fileout.write(outerZipBytes);
fileout.close();
outerZipFile = new ZipFile(zipFile);
File innerZipFile = getInnerZipFile(outerZipFile);
addFilesToInnerZip(innerZipFile, files);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (outerZipFile != null)
outerZipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// TODO convert updated outer zip file to byte array
return new byte[0];
}
private File getInnerZipFile(ZipFile outerZipFile) throws IOException {
String innerZipPath = System.getProperty("catalina.home") + File.separator + "innerZipFile.zip";
ZipEntry entry = outerZipFile.getEntry("folder/inner.zip");
InputStream innerZipInputStream = outerZipFile.getInputStream(entry);
FileOutputStream fout = new FileOutputStream(new File(innerZipPath));
byte[] buf = new byte[1024];
int data;
while ((data = innerZipInputStream.read(buf)) != -1) {
fout.write(buf, 0, data);
}
innerZipInputStream.close();
fout.close();
return new File(innerZipPath);
}
private void addFilesToInnerZip(File zipFile, Set<File> files) throws IOException {
File tempFile = File.createTempFile(zipFile.getName(), null);
tempFile.delete();
zipFile.renameTo(tempFile);
byte[] buf = new byte[1024];
ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
ZipEntry entry = zin.getNextEntry();
while (entry != null) {
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(entry.getName()));
// Transfer bytes from the ZIP file to the output file
int len;
while ((len = zin.read(buf)) > 0) { // line 179
out.write(buf, 0, len);
}
entry = zin.getNextEntry();
}
for (File file : files) {
InputStream in = new FileInputStream(file);
out.putNextEntry(new ZipEntry("innerFolder/"+ file.getName()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
zin.close();
out.close();
tempFile.delete();
}
Thanks for any help!

Java // unzip error :MALFORMED

I would like to unzip recursively some archive .zip.
I use java.util.zip and I can't use an other library.
My code :
public static void unzip(String file) {
try {
File fSourceZip = new File(file);
String zipPath = file.substring(0, file.length() - 4);
File temp = new File(zipPath);
temp.mkdir();
System.out.println(zipPath + " created");
ZipFile zipFile = new ZipFile(fSourceZip);
Enumeration e = zipFile.entries();
while (e.hasMoreElements()) {
ZipEntry entry = (ZipEntry) e.nextElement();
File destinationFilePath = new File(zipPath, entry.getName());
destinationFilePath.getParentFile().mkdirs();
if (entry.isDirectory()) {
continue;
} else {
System.out.println("Extracting " + destinationFilePath);
BufferedInputStream bis = new BufferedInputStream(
zipFile.getInputStream(entry));
int b;
byte buffer[] = new byte[1024];
FileOutputStream fos = new FileOutputStream(
destinationFilePath);
BufferedOutputStream bos = new BufferedOutputStream(fos,
1024);
while ((b = bis.read(buffer, 0, 1024)) != -1) {
bos.write(buffer, 0, b);
}
bos.flush();
bos.close();
bis.close();
}
if (entry.getName().endsWith(".zip")) {
// found a zip file, try to open
unzip(destinationFilePath.getAbsolutePath());
}
}
} catch (IOException ioe) {
System.out.println("IOError :" + ioe);
}
}
But I have some error with some archive :
Exception in thread "main" java.lang.IllegalArgumentException: MALFORMED
at java.util.zip.ZipCoder.toString(ZipCoder.java:58)
at java.util.zip.ZipFile.getZipEntry(ZipFile.java:567)
at java.util.zip.ZipFile.access$900(ZipFile.java:61)
at java.util.zip.ZipFile$ZipEntryIterator.next(ZipFile.java:525)
at java.util.zip.ZipFile$ZipEntryIterator.nextElement(ZipFile.java:500)
at java.util.zip.ZipFile$ZipEntryIterator.nextElement(ZipFile.java:481)
at zip.ReadingArchive.unzip(ReadingArchive.java:36)
at zip.ReadingArchive.unzip(ReadingArchive.java:82)
at zip.ReadingArchive.unzip(ReadingArchive.java:82)
at main.Main.main(Main.java:13)
I have this problem because there is .odp in my archive. How I can say only uses .zip, not other files ?
How I can resolve this problem ?
Thanks !
I've just fixed it by specifying alternative (non UTF-8) charset:
Charset CP866 = Charset.forName("CP866");
ZipFile zipFile = new ZipFile(zipArchive, CP866);
In your case, you need to specify another charset. Try, CP437, for instance.

How do you unzip a zip in java? [duplicate]

This question already has answers here:
zip and unzip in java
(2 answers)
Closed 10 years ago.
I'm wondering, how do you unzip a zip file in java?
I first tried:
String source = "forge.zip";
String destination = "some/destination/folder";
try {
zipFile = new ZipFile(source);
zipFile.extractAll(destination);
} catch (ZipException e) {
e.printStackTrace();
}
It gives says that zpFile does not have an extractAll method.
Is that true?
I cannot tell what problem you're having. But I have done this before several times with ZipInputStreams and ZipEntrys, and I know this will work:
File dir = new File(destDir);
if(!dir.exists()) dir.mkdirs();
FileInputStream fis;
byte[] buffer = new byte[1024];
try {
fis = new FileInputStream(zipFilePath);
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry ze = zis.getNextEntry();
while(ze != null){
String fileName = ze.getName();
File newFile = new File(destDir + File.separator + fileName);
new File(newFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
zis.closeEntry();
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}

Categories