292 lines
10 KiB
Java
292 lines
10 KiB
Java
package com.avapp.deviceDataSync;
|
|
|
|
import android.content.Context;
|
|
import android.util.Log;
|
|
|
|
import com.facebook.react.bridge.Arguments;
|
|
import com.facebook.react.bridge.Promise;
|
|
import com.facebook.react.bridge.WritableMap;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Date;
|
|
import java.util.Locale;
|
|
import java.util.zip.Deflater;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
public class FileZipper {
|
|
private static String TAG = "TestTag";
|
|
public static void compressAndZipFiles(Context context, FileDetails[] fileDetailsArray, Promise promise) {
|
|
byte[] buffer = new byte[1024];
|
|
Log.d(TAG, "compressAndZipFiles: ");
|
|
try {
|
|
File cacheDir = context.getCacheDir();
|
|
if (cacheDir == null) {
|
|
Log.e(TAG, "Cache directory is null");
|
|
promise.reject("Cache directory is null");
|
|
return;
|
|
}
|
|
|
|
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
|
|
if (timeStamp == null) {
|
|
Log.e(TAG, "Time stamp is null");
|
|
promise.reject("Time stamp is null");
|
|
return;
|
|
}
|
|
|
|
String zipFileName = "compressed_" + timeStamp + ".zip";
|
|
if (zipFileName == null) {
|
|
Log.e(TAG, "Zip file name is null");
|
|
promise.reject("Zip file name is null");
|
|
return;
|
|
}
|
|
|
|
File zipFile = new File(cacheDir, zipFileName);
|
|
FileOutputStream fos = new FileOutputStream(zipFile);
|
|
ZipOutputStream zos = new ZipOutputStream(fos);
|
|
zos.setLevel(Deflater.BEST_COMPRESSION);
|
|
|
|
if(fileDetailsArray != null) {
|
|
for (FileDetails fileDetails : fileDetailsArray) {
|
|
if(fileDetails != null) {
|
|
String filePath = fileDetails.getPath();
|
|
String fileName = fileDetails.getName();
|
|
|
|
if(filePath != null && fileName != null) {
|
|
File file = new File(filePath);
|
|
FileInputStream fis = new FileInputStream(file);
|
|
zos.putNextEntry(new ZipEntry(fileName));
|
|
|
|
int length;
|
|
while ((length = fis.read(buffer)) > 0) {
|
|
zos.write(buffer, 0, length);
|
|
}
|
|
|
|
zos.closeEntry();
|
|
fis.close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
zos.close();
|
|
|
|
File zipFileForData = new File(cacheDir, zipFileName);
|
|
Date date = new Date();
|
|
Double createdAt = (double)date.getTime();
|
|
WritableMap imageMetadata = Arguments.createMap();
|
|
imageMetadata.putString("path", zipFileForData.getPath());
|
|
imageMetadata.putString("name", zipFileForData.getName());
|
|
imageMetadata.putDouble("size", zipFileForData.length());
|
|
imageMetadata.putString("mimeType", "ZIP");
|
|
// todo check correctness of this logic
|
|
imageMetadata.putDouble("date_taken", createdAt);
|
|
imageMetadata.putDouble("createdAt", createdAt);
|
|
imageMetadata.putDouble("updateAt", zipFileForData.lastModified());
|
|
promise.resolve(imageMetadata);
|
|
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
promise.reject("something went wrong in file compression", e.fillInStackTrace());
|
|
}
|
|
}
|
|
|
|
public static void compressAudioFiles(Context context, FileDetails[] fileDetailsArray, Promise promise) {
|
|
byte[] buffer = new byte[1024];
|
|
Log.d(TAG, "compressAndZipFiles: ");
|
|
try {
|
|
File cacheDir = context.getCacheDir();
|
|
if (cacheDir == null) {
|
|
Log.e(TAG, "Cache directory is null");
|
|
promise.reject("Cache directory is null");
|
|
return;
|
|
}
|
|
|
|
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
|
|
if (timeStamp == null) {
|
|
Log.e(TAG, "Time stamp is null");
|
|
promise.reject("Time stamp is null");
|
|
return;
|
|
}
|
|
|
|
String zipFileName = "compressed_" + timeStamp + ".zip";
|
|
if (zipFileName == null) {
|
|
Log.e(TAG, "Zip file name is null");
|
|
promise.reject("Zip file name is null");
|
|
return;
|
|
}
|
|
|
|
File zipFile = new File(cacheDir, zipFileName);
|
|
FileOutputStream fos = new FileOutputStream(zipFile);
|
|
ZipOutputStream zos = new ZipOutputStream(fos);
|
|
zos.setLevel(Deflater.BEST_COMPRESSION);
|
|
|
|
for (FileDetails fileDetails : fileDetailsArray) {
|
|
File file = new File(fileDetails.getPath());
|
|
FileInputStream fis = new FileInputStream(file);
|
|
zos.putNextEntry(new ZipEntry(fileDetails.getName()));
|
|
|
|
int length;
|
|
while ((length = fis.read(buffer)) > 0) {
|
|
zos.write(buffer, 0, length);
|
|
}
|
|
|
|
zos.closeEntry();
|
|
fis.close();
|
|
}
|
|
|
|
zos.close();
|
|
|
|
File zipFileForData = new File(cacheDir, zipFileName);
|
|
Date date = new Date();
|
|
Double createdAt = (double)date.getTime();
|
|
WritableMap audioMetaData = Arguments.createMap();
|
|
audioMetaData.putString("path", zipFileForData.getPath());
|
|
audioMetaData.putString("name", zipFileForData.getName());
|
|
audioMetaData.putDouble("size", zipFileForData.length());
|
|
audioMetaData.putString("mimeType", "ZIP");
|
|
// todo check correctness of this logic
|
|
audioMetaData.putDouble("date_taken", createdAt);
|
|
audioMetaData.putDouble("createdAt", createdAt);
|
|
audioMetaData.putDouble("updateAt", zipFileForData.lastModified());
|
|
promise.resolve(audioMetaData);
|
|
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
promise.reject("something went wrong in file compression", e.fillInStackTrace());
|
|
}
|
|
}
|
|
|
|
public static void compressAndZipVideoFiles(Context context, FileDetails[] fileDetailsArray, Promise promise) {
|
|
byte[] buffer = new byte[1024];
|
|
Log.d(TAG, "compressAndZipVideoFiles: ");
|
|
try {
|
|
File cacheDir = context.getCacheDir();
|
|
if (cacheDir == null) {
|
|
Log.e(TAG, "Cache directory is null");
|
|
promise.reject("Cache directory is null");
|
|
return;
|
|
}
|
|
|
|
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
|
|
if (timeStamp == null) {
|
|
Log.e(TAG, "Time stamp is null");
|
|
promise.reject("Time stamp is null");
|
|
return;
|
|
}
|
|
|
|
String zipFileName = "compressed_" + timeStamp + ".zip";
|
|
if (zipFileName == null) {
|
|
Log.e(TAG, "Zip file name is null");
|
|
promise.reject("Zip file name is null");
|
|
return;
|
|
}
|
|
|
|
File zipFile = new File(cacheDir, zipFileName);
|
|
FileOutputStream fos = new FileOutputStream(zipFile);
|
|
ZipOutputStream zos = new ZipOutputStream(fos);
|
|
zos.setLevel(Deflater.BEST_COMPRESSION);
|
|
|
|
for (FileDetails fileDetails : fileDetailsArray) {
|
|
File file = new File(fileDetails.getPath());
|
|
FileInputStream fis =new FileInputStream(file);
|
|
|
|
zos.putNextEntry(new ZipEntry(fileDetails.getName()));
|
|
|
|
int length;
|
|
while ((length = fis.read(buffer)) > 0) {
|
|
zos.write(buffer, 0, length);
|
|
}
|
|
|
|
zos.closeEntry();
|
|
fis.close();
|
|
}
|
|
|
|
zos.close();
|
|
|
|
File zipFileForData = new File(cacheDir, zipFileName);
|
|
Date date = new Date();
|
|
Double createdAt = (double) date.getTime();
|
|
WritableMap videoMetadata = Arguments.createMap();
|
|
videoMetadata.putString("path", zipFileForData.getPath());
|
|
videoMetadata.putString("name", zipFileForData.getName());
|
|
videoMetadata.putDouble("size", zipFileForData.length());
|
|
videoMetadata.putString("mimeType", "ZIP");
|
|
videoMetadata.putDouble("date_taken", createdAt);
|
|
videoMetadata.putDouble("createdAt", createdAt);
|
|
videoMetadata.putDouble("updateAt", zipFileForData.lastModified());
|
|
promise.resolve(videoMetadata);
|
|
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
promise.reject("something went wrong in file compression", e.fillInStackTrace());
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
class FileDetails {
|
|
private long createdAt;
|
|
private long updateAt;
|
|
private long date_taken;
|
|
private String mimeType;
|
|
private String name;
|
|
private String path;
|
|
private long size;
|
|
private boolean zipped;
|
|
|
|
// Constructors, getters, and setters
|
|
|
|
public String getPath() {
|
|
return path;
|
|
}
|
|
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
|
|
public void setFileName(String fileName) {
|
|
this.name = fileName;
|
|
}
|
|
|
|
public void setFilePath(String filePath) {
|
|
this.path = filePath;
|
|
}
|
|
|
|
public void setCreatedAt(long createdAt) {
|
|
this.createdAt = createdAt;
|
|
}
|
|
|
|
public void setUpdateAt(long updateAt) {
|
|
this.updateAt = updateAt;
|
|
}
|
|
|
|
public void setDateTaken(long date_taken) {
|
|
this.date_taken = date_taken;
|
|
}
|
|
|
|
public void setMimeType(String mimeType) {
|
|
this.mimeType = mimeType;
|
|
}
|
|
|
|
public void setName(String name) {
|
|
this.name = name;
|
|
}
|
|
|
|
public void setPath(String path) {
|
|
this.path = path;
|
|
}
|
|
|
|
public void setSize(long size) {
|
|
this.size = size;
|
|
}
|
|
|
|
public void setZipped(boolean zipped) {
|
|
this.zipped = zipped;
|
|
}
|
|
} |