Java file zip is creating corrupt files - java

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();

Related

Getting blank zip file when getting created

I am getting blank zip even though I am writing images to it .Can some one let me know what I am doing wrong?I am trying to add images to the zip file .Its creating a zip file but with blank entry on it .so why its not writing the file to it?
int i = 0;
List<String> filenames = new ArrayList<String>();
for (Iterator iterator = datas.iterator(); iterator.hasNext();) {
byte[] bs = (byte[]) iterator.next();
BufferedImage image = null;
ByteArrayInputStream bis = new ByteArrayInputStream(bs);
image = ImageIO.read(bis);
bis.close();
// write the image to a file
File outputfile = new File("image" + i + ".jpg");
System.out.println("*************************" + outputfile.getAbsolutePath());
ImageIO.write(image, "jpg", outputfile);
i++;
filenames.add(outputfile.getAbsolutePath());
}
// Create the ZIP file
//create object of FileOutputStream
FileOutputStream fout = new FileOutputStream(zipName);
//create object of ZipOutputStream from FileOutputStream
ZipOutputStream zout = new ZipOutputStream(fout);
byte[] buffer = new byte[2048];
for (int k = 0; k < filenames.size(); k++) {
FileInputStream fis = new FileInputStream(filenames.get(k).toString());
// Add ZIP entry to output stream.
File file = new File(filenames.get(k).toString());
String entryname = file.getName();
zout.putNextEntry(new ZipEntry(entryname));
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
zout.write(buffer, 0, bytesRead);
}
zout.closeEntry();
fis.close();
}
zout.flush();
fout.flush();
zout.close();
fout.close();

Java not writing to zip file

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());

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!

ZipOutPutStream throws StringIndexOutOfBoundsException

I am trying to Zip file using ZipOutputStream but it's throwing StringIndexOutOfBoundsException while passing a FileOutPutStream object.
The code was working fine when I ran it as a separate java class but when I integrated it with my project, it's throwing the exception.
I used this code:
byte[] buffer = new byte[1024];
try{
FileOutputStream fos = new FileOutputStream("C:\\Report.zip");
//error throwing at this point (java.lang.StringIndexOutOfBoundsException)
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
FileInputStream in = new FileInputStream("C:\\Report.xls");
zos.putNextEntry(new ZipEntry("Report.xls"));
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
zos.closeEntry();
zos.close();
}catch(IOException ex){
Log.report.debug(ex.toString());
}

Categories