Getting blank zip file when getting created - java

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

Related

Does this block select an image or its path?

I understand that this code finds a photo via path but I don't understand what is in the photo variable
fileName = f.getAbsolutePath();
try {
File image2 = new File(fileName);
FileInputStream fis = new FileInputStream(image2);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for(int readNum; (readNum=fis.read(buf))!=-1;) {
bos.write(buf, 0, readNum);
}
photo = bos.toByteArray();
} catch(Exception e1) {
JOptionPane.showMessageDialog(null, e1);
}
The below code is just getting the byte array of that photo , that means the content of photo in byte array format.
fileName = f.getAbsolutePath();
try {
//Getting file object from the filePath
File image2 = new File(fileName);
// create input stream for that image object
FileInputStream fis = new FileInputStream(image2);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
// below repeated logic read the fis object till its empty and write into byte array object
for(int readNum; (readNum=fis.read(buf))!=-1;) {
bos.write(buf, 0, readNum);
}
photo = bos.toByteArray();
} catch(Exception e1) {
JOptionPane.showMessageDialog(null, e1);
}

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

ANDROID GC_FOR_ALLOC freed in my app going wrong

That's my android code which takes 30 min to copy a 3MB .log file into a .zip and gives lots of GC_FOR_ALLOC. I also tried to change buffersize from 1k to 8k
File tempFolder=new File(Environment.getExternalStorageDirectory()+LOG_FILE_DIRECTORY_TEMP);
String filePath= Environment.getExternalStorageDirectory()+LOG_FILE_DIRECTORY_TEMP+ "/";
String fileName ="";
String zipFileName="";
String date=DATE_FORMAT_TEMP.format(new Date());
fileName = Settings.LOG_FILE_PREFIX + date+"_" + IMEI +.log;
zipFileName = Settings.LOG_FILE_PREFIX + date+"_" + IMEI +.zip;
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String result="";
String line = "";
while((line = br.readLine())!=null)
result += line;
result = getHeader() + result;
fos = new FileOutputStream(file);
fos.write(result.getBytes());
File zipFile=new File(filePath+zipFileName);
iStream = new FileInputStream(file);
oStream = new FileOutputStream(zipFile);
zos = new ZipOutputStream(oStream);
ze = new ZipEntry(fileName);
zos.putNextEntry(ze);
byte[] buffer = new byte[1024];
while((length = iStream.read(buffer)) != -1)
zos.write(buffer, 0, length);
zos.flush();
oStream.flush();
In your code it looks like you are opening and reading the file twice (your BufferedReader and your iStream object). Also, you are loading the entire file into memory twice before writing anything to memory. That's still only 6MB but you probably are hitting your memory stack limit - unless you use android:largeHeap="true" in your manifest.
Before you do that though, try just reading/writing each part:
public void zip(String[] _files, String zipFileName) {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(zipFileName);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
dest));
byte data[] = new byte[BUFFER];
for (int i = 0; i < _files.length; i++) {
Log.v("Compress", "Adding: " + _files[i]);
FileInputStream fi = new FileInputStream(_files[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1));
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();
}
}
Here is the reference: http://javatechig.com/android/how-to-programmatically-zip-and-unzip-file-in-android

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

Create ZIP from byte array [duplicate]

I am trying to convert an array of bytes into a ZIP file. I got bytes using the following code:
byte[] originalContentBytes= new Verification().readBytesFromAFile(new File("E://file.zip"));
private byte[] readBytesFromAFile(File file) {
int start = 0;
int length = 1024;
int offset = -1;
byte[] buffer = new byte[length];
try {
//convert the file content into a byte array
FileInputStream fileInuptStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(
fileInuptStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while ((offset = bufferedInputStream.read(buffer, start, length)) != -1) {
byteArrayOutputStream.write(buffer, start, offset);
}
bufferedInputStream.close();
byteArrayOutputStream.flush();
buffer = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
return buffer;
}
But my problem now is with converting the byte array back into a ZIP file - how can it be done?
Note : The specified ZIP contains two files.
To get the contents from the bytes you can use
ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(bytes));
ZipEntry entry = null;
while ((entry = zipStream.getNextEntry()) != null) {
String entryName = entry.getName();
FileOutputStream out = new FileOutputStream(entryName);
byte[] byteBuff = new byte[4096];
int bytesRead = 0;
while ((bytesRead = zipStream.read(byteBuff)) != -1)
{
out.write(byteBuff, 0, bytesRead);
}
out.close();
zipStream.closeEntry();
}
zipStream.close();
You probably are looking for code like this:
ZipInputStream z = new ZipInputStream(new ByteArrayInputStream(buffer))
now you can get the zip file contents via getNextEntry()
Here is a helper method
private fun getZipData(): ByteArray {
val zipFile: File = getTempZipFile() // Return a zip File
val encoded = Files.readAllBytes(Paths.get(zipFile.absolutePath))
zipFile.delete() // If you wish to delete the zip file
return encoded
}

Categories