Android: Export images to zip - android

Could someone help me, in my application I would like to export my images from gallery to .zip, but I have no idea what I need to do that. I will be grateful for any tips

This is some basic zip code. Essentially create a string array containing all the image file paths you want to include in the zip and another string for the destination path of your zip file. Pass these on to the method.
public static void zip(String[] files, String zipFile) throws IOException {
BufferedInputStream origin = null;
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
try {
byte data[] = new byte[BUFFER_SIZE];
for (int i = 0; i < files.length; i++) {
FileInputStream fi = new FileInputStream(files[i]);
origin = new BufferedInputStream(fi, BUFFER_SIZE);
try {
ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1));
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
out.write(data, 0, count);
}
}
finally {
origin.close();
}
}
}
finally {
out.close();
}
}
Or alternatively, you can use the Zip4j library.
Happy coding! :D

Use "
Easy Unrar, Unzip & Zip
"
Find the directory of the gallery, compress it
EDITED
Try this:
public static void compress(String[] folder, String compFile) throws IOException {
BufferedInputStream origin = null;
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(compFile)));
try {
byte data[] = new byte[BUFFER_SIZE];
for (int i = 0; i < folder.length; i++) {
FileInputStream fi = new FileInputStream(folder[i]);
origin = new BufferedInputStream(fi, BUFFER_SIZE);
try {
ZipEntry entry = new ZipEntry(folder[i].substring(folder[i].lastIndexOf("/") + 1));
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
out.write(data, 0, count);
}
}
finally {
origin.close();
}
}
}
finally {
out.close();
}
}

Related

While zip multiple images the images size and quality gets reduced , how to solve that?

I have used below function to zip multiple images from my folder, mainViewModel.getImagesList() will return array of images path which i want to added in zip folder
public void compressImagesToZip() throws IOException {
BufferedInputStream origin = null;
String opd = Environment.getExternalStorageDirectory() + "/" + getApplication().getApplicationContext().getResources().getString(R.string.imageFolder) + "/GroTrack.zip";
Log.e("====>>", opd);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(opd)));
try {
byte data[] = new byte[2048];
for (int i = 0; i < mainViewModel.getImagesList().size(); i++) {
FileInputStream fi = new FileInputStream(mainViewModel.getImagesList().get(i));
origin = new BufferedInputStream(fi, 2048);
try {
ZipEntry entry = new ZipEntry(mainViewModel.getImagesList().get(i).substring(mainViewModel.getImagesList().get(i).lastIndexOf("/") + 1));
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, 2048)) != -1) {
out.write(data, 0, count);
}
} finally {
origin.close();
}
}
} finally {
out.close();
}
}
Please refere this images, before zip and after zip image sizes were different
before
after

Android ZIP folders

I use this code to ZIP folders including inner folders :
public boolean zipFileAtPath(String sourcePath, String toLocation) {
// ArrayList<String> contentList = new ArrayList<String>();
final int BUFFER = 2048;
File sourceFile = new File(sourcePath);
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(toLocation);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
dest));
if (sourceFile.isDirectory()) {
zipSubFolder(out, sourceFile, sourceFile.getParent().length());
} else {
byte data[] = new byte[BUFFER];
FileInputStream fi = new FileInputStream(sourcePath);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(getLastPathComponent(sourcePath));
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
}
out.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
private void zipSubFolder(ZipOutputStream out, File folder,
int basePathLength) throws IOException {
final int BUFFER = 2048;
File[] fileList = folder.listFiles();
BufferedInputStream origin = null;
for (File file : fileList) {
if (file.isDirectory()) {
zipSubFolder(out, file, basePathLength);
} else {
byte data[] = new byte[BUFFER];
String unmodifiedFilePath = file.getPath();
String relativePath = unmodifiedFilePath
.substring(basePathLength);
Log.i("ZIP SUBFOLDER", "Relative Path : " + relativePath);
FileInputStream fi = new FileInputStream(unmodifiedFilePath);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(relativePath);
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
}
}
public String getLastPathComponent(String filePath) {
String[] segments = filePath.split("/");
String lastPathComponent = segments[segments.length - 1];
return lastPathComponent;
}
It works fine when folder looks like :
Folder
txt file
jpg file
txt file
It also works fine when folder looks like :
Folder
txt file
Folder
txt file
txt file
However in this case :
Folder
txt file
Folder
txt file
jpg file
txt file
it starts endless loop, when ZIP file continuously increases.
I noted, that it happens because this loop :
private void zipSubFolder(ZipOutputStream out, File folder,
int basePathLength) throws IOException {
.....
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
....
}
doesn't stop in the case, described above.
Any ideas why this can happen ? Thanks )
Here is the solution.
I save ZIP file inside the same folder, that is zipped. So it starts to zip the ZIP file itself, saving new ZIP file inside again and again and again.
Not sure, if it's clear, but anyway, pay attention where do you save ZIP file.

Ways to compress resources and/or files in Asset folders?

I am approaching finishing my application. My asset folder has over 20 PDF files in it, and obviously they are taking up quite a bit of space. Is there a way I can compress these files to make the Application smaller? What are other good techniques for checking / lowering memory consumption in my code / throughout my project? Thanks in advance for any input.
You could zip your files then decrompress when needed
Here's a tutorial Zip programmatically with Android
Check the java.util.zip class , it provides both zip & gzip functionality for compression and decompression.
You could also download the pdfs from another site once the application is installed
Or This Should work:
/*
*
* Zips a file at a location and places the resulting zip file at the toLocation
* Example: zipFileAtPath("downloads/myfolder", "downloads/myFolder.zip");
*/
public boolean zipFileAtPath(String sourcePath, String toLocation) {
// ArrayList<String> contentList = new ArrayList<String>();
final int BUFFER = 2048;
File sourceFile = new File(sourcePath);
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(toLocation);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
dest));
if (sourceFile.isDirectory()) {
zipSubFolder(out, sourceFile, sourceFile.getParent().length());
} else {
byte data[] = new byte[BUFFER];
FileInputStream fi = new FileInputStream(sourcePath);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(getLastPathComponent(sourcePath));
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
}
out.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/*
*
* Zips a subfolder
*
*/
private void zipSubFolder(ZipOutputStream out, File folder,
int basePathLength) throws IOException {
final int BUFFER = 2048;
File[] fileList = folder.listFiles();
BufferedInputStream origin = null;
for (File file : fileList) {
if (file.isDirectory()) {
zipSubFolder(out, file, basePathLength);
} else {
byte data[] = new byte[BUFFER];
String unmodifiedFilePath = file.getPath();
String relativePath = unmodifiedFilePath
.substring(basePathLength);
Log.i("ZIP SUBFOLDER", "Relative Path : " + relativePath);
FileInputStream fi = new FileInputStream(unmodifiedFilePath);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(relativePath);
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
}
}
/*
* gets the last path component
*
* Example: getLastPathComponent("downloads/example/fileToZip");
* Result: "fileToZip"
*/
public String getLastPathComponent(String filePath) {
String[] segments = filePath.split("/");
String lastPathComponent = segments[segments.length - 1];
return lastPathComponent;
}

How to zip and unzip png images in android

Hi in my app when i click zip button i need to zip image file and and when i click unzip button i need to unzip file,i tried using below code to zip image but my problem is when i click zip button zip file is creating,but after that in system using winzip software i try to open file but its not opening its showing "it does not appear to a valid archive valid file" where i did mistake can u let me how to zip and unzip images
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button zip,unzip;
String []s=new String[2];
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
zip=(Button)findViewById(R.id.button1);
zip.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
s[0]="/sdcard/saved_images/Main.png";
//s[1]="/sdcard/Physics_Lab/Stefans_Law/stefan_law.txt"; // path of the second file
Compress c =new Compress(s,"/sdcard/saved_images/stefen.zip");
c.zip();
}
});
}
}
public class Compress {
private static final int BUFFER = 80000;
private String[] _files;
private String _zipFile;
public Compress(String[] files, String zipFile) {
_files = files;
_zipFile = zipFile;
}
public void zip() {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(_zipFile);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
byte data[] = new byte[BUFFER];
for(int i=0; i < _files.length; i++) {
Log.d("add:",_files[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();
}
}
}
You can check out these 2 tutorials to zip and unzip the file
Android Zip file
Android Unzip file
You can use ZipOutputStream in Java
See API Document Here
You can create the zip file by follow function
OutputStream os = ...
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(os));
try {
for (int i = 0; i < fileCount; ++i) {
String filename = ...
byte[] bytes = ...
ZipEntry entry = new ZipEntry(filename);
zos.putNextEntry(entry);
zos.write(bytes);
zos.closeEntry();
}
} finally {
zos.close();
}
For Open a zip file, you can use ZipInputStream
Api Document here
InputStream is = ...
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;
while ((count = zis.read(buffer)) != -1) {
baos.write(buffer, 0, count);
}
String filename = ze.getName();
byte[] bytes = baos.toByteArray();
// do something with 'filename' and 'bytes'...
}
} finally {
zis.close();
}

How to only zip a folder and files contained in a particular folder in Android?

I am able to zip files contained in a particular folder. This is the code that I have used:
public class Compress {
private static final int BUFFER = 2048;
private String[] _files;
private String _zipFile;
public Compress(String[] files, String zipFile) {
_files = files;
_zipFile = zipFile;
}
public void zip() {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(_zipFile);
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();
}
}
}
I'm calling this class in this way in another class:
String[] files = {mainFolderPath+"/text1.txt", mainFolderPath+ "/text2.txt", mainFolderPath +"/NewFolder"};
Compress compress = new Compress(files, sourceFile.getAbsolutePath());
compress.zip();
On running the application I'm getting an IOException.
Could you please tell me how to zip the "NewFolder" which contains another text file along with the text files "text1.txt" and "text2.txt"?
Thank you.
It works for me!!!!!!!! in all my projects I need....
public String[] _files;
public String _zipFile = "/mnt/sdcard/123.zip";
/////zIIIiiiiPPPPPer
public class Compress {
private static final int BUFFER = 2048;
public Compress(String[] files, String zipFile) {
_files = files;
_zipFile = zipFile;
}
public void zip() {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(_zipFile);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
String path = "/mnt/sdcard/out";
File fileDir = new File( path );
if(!fileDir.exists() || !fileDir.isDirectory())
{
return;
}
String[] _files = fileDir.list();
for ( int i = 0 ; i < _files.length ; i++ )
{
_files[i] = path + "/"+ _files[i];
}
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();
}
}
To call it JuSt do smth like...
Compress cs = new Compress(_files, _zipFile);
I got the solution for my question ie; zipping only the contents of a particular folder which includes a folder along with some text files. This is the code:
public class Compress {
private static final int BUFFER = 2048;
private String[] _files;
private String _zipFile;
public Compress(String[] files, String zipFile) {
_files = files;
_zipFile = zipFile;
}
public void zip() {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(_zipFile);
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]);
String name = _files[i];
File f = new File( _files[i]);
if (f.isDirectory()) {
name = name.endsWith("/") ? name : name + "/";
for (String file : f.list()) {
System.out.println(" checking " + file);
System.out
.println("The folder name is: " + f.getName());
out.putNextEntry(new ZipEntry(f.getName() + "/" + file));
FileInputStream fi = new FileInputStream( _files[i]
+ "/" + file);
origin = new BufferedInputStream(fi, BUFFER);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
System.out.println(" checking folder" + name);
} else {
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();
}
}
}

Categories

Resources