Folder are file not zipping - java

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

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

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

Unzip to internal storage not working completely

My file unzips one folder and one file but does not unzip the rest for some reason. How can I make it so it unzips all of the zip files contents?
public void unzip(String filepath, String filename, String unzip_path) throws IOException {
InputStream is = new FileInputStream(filepath + filename);
Log.d("1st", filepath + filename);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
try {
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count;
String filename_temp = ze.getName();
File fmd = new File(unzip_path + filename_temp);
Log.d("2nd", unzip_path + filename_temp);
if (!fmd.getParentFile().exists()) {
fmd.getParentFile().mkdirs();
}
FileOutputStream fout = new FileOutputStream(unzip_path + filename_temp);
while ((count = zis.read(buffer)) != -1) {
baos.write(buffer, 0, count);
byte[] bytes = baos.toByteArray();
fout.write(bytes);
baos.reset();
}
fout.close();
//}
}
} finally {
zis.close();
}
}

Categories