Skip to content

Commit

Permalink
feat(android): compression level
Browse files Browse the repository at this point in the history
  • Loading branch information
m1ga committed Sep 17, 2022
1 parent cb09302 commit fde2abe
Show file tree
Hide file tree
Showing 14 changed files with 274 additions and 164 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,31 @@ To extract an archive:
var result = Ti.Compression.unzip(Ti.Filesystem.applicationDataDirectory + 'data', 'test.zip', true);
```
See example for more details.
## zip Parameter
Android:
* filename:String, files:Array, options: Object [compression: Compression.BEST_SPEED| Compression.DEFAULT_COMPRESSION | Compression.NO_COMPRESSION]
iOS:
* filename:String, files:Array
## Create demo files
Create demo files
good compression (no content)
```bash
truncate -s 5M file1.dat
```
Bad compression (random content)
```bash
dd if=/dev/urandom of=file1 bs=5M count=1
```
## Author
Clint Tredway
Expand Down
5 changes: 5 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

dependencies {
// Add the module's library dependencies here.
// See: https://developer.android.com/studio/build/dependencies
}
2 changes: 1 addition & 1 deletion android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 5.0.0
version: 6.0.0
apiversion: 4
architectures: arm64-v8a armeabi-v7a x86 x86_64
description: Lets you zip and unzip files.
Expand Down
321 changes: 170 additions & 151 deletions android/src/ti/compression/CompressionModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,168 +6,187 @@

package ti.compression;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.io.TiBaseFile;
import org.appcelerator.titanium.io.TiFileFactory;
import org.appcelerator.titanium.util.TiConvert;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.zip.CRC32;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.io.TiBaseFile;
import org.appcelerator.titanium.io.TiFileFactory;

@Kroll.module(name = "Compression", id = "ti.compression")
public class CompressionModule extends KrollModule
{

public CompressionModule()
{
super();
}

@Kroll.method
public String zip(Object[] args)
{
// Check that our arguments are valid.
if (args.length != 2) {
return "Invalid number of arguments provided!";
}

String rawZip = (String) args[0];
if (rawZip == null || rawZip.length() == 0) {
return "archiveFile was not specified or was empty!";
}
TiBaseFile zip = TiFileFactory.createTitaniumFile(rawZip, false);

Object[] rawFiles = (Object[]) args[1];
if (rawFiles == null || rawFiles.length == 0) {
return "fileArray was not specified or was empty!";
}
LinkedList<TiBaseFile> files = new LinkedList<TiBaseFile>();
for (Object rawFile : rawFiles) {
files.add(TiFileFactory.createTitaniumFile(rawFile.toString(), false));
}

// And then zip the files in to the archive.
try {
OutputStream fout = zip.getOutputStream();
ZipOutputStream zout = new ZipOutputStream(new BufferedOutputStream(fout));

while (!files.isEmpty()) {
TiBaseFile file = files.remove();
if (!file.exists()) {
Util.e("Skipping over file, because it does not exist: " + file.nativePath());
} else {
ZipEntry ze = new ZipEntry(file.name());
zout.putNextEntry(ze);
writeInFile(file, zout);
zout.closeEntry();
}
}
zout.close();
return "success";
} catch (Exception e) {
Util.e("Hit exception while unzipping the archive!", e);
return e.toString();
}
}

@Kroll.method
public String unzip(Object[] args)
{
// Check that our arguments are valid.
if (args.length != 3) {
return "Invalid number of arguments provided!";
}

String rawDest = (String) args[0];
if (rawDest == null || rawDest.length() == 0) {
return "destinationFolder was not specified or was empty!";
}
String destPath = TiFileFactory.createTitaniumFile(rawDest, false).getNativeFile().getAbsolutePath();

String rawZip = (String) args[1];
if (rawZip == null || rawZip.length() == 0) {
return "archiveFile was not specified or was empty!";
}
TiBaseFile zip = TiFileFactory.createTitaniumFile(rawZip, false);
if (!zip.exists())
return "archiveFile was not found at " + rawZip + "!";

Boolean overwrite = (Boolean) args[2];
if (overwrite == null) {
return "overwrite was not specified!";
}

// And then unzip the archive.
try {
InputStream fin = zip.getInputStream();
ZipInputStream zin = new ZipInputStream(new BufferedInputStream(fin));
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
String target = destPath + "/" + ze.getName();
if (ze.isDirectory()) {
ensureDirectoryExists(target);
} else {
File file = new File(target);
if (overwrite || !file.exists()) {
writeOutFile(file, target, zin);
}
}
}
zin.close();
fin.close();
return "success";
} catch (Exception e) {
Util.e("Hit exception while unzipping the archive!", e);
return e.toString();
}
}

public void ensureDirectoryExists(String target)
{
File f = new File(target);
if (!f.isDirectory()) {
f.mkdirs();
}
}

private void writeInFile(TiBaseFile tifile, ZipOutputStream zout) throws IOException
{
int size;
byte[] buffer = new byte[2048];
InputStream fos = tifile.getInputStream();
BufferedInputStream bos = new BufferedInputStream(fos, buffer.length);
while ((size = bos.read(buffer, 0, buffer.length)) != -1) {
zout.write(buffer, 0, size);
}
}

private void writeOutFile(File file, String target, ZipInputStream zin) throws IOException
{
// Make sure the parent directory exists.
file.getParentFile().mkdirs();
// Write out the file.
int size;
byte[] buffer = new byte[2048];
FileOutputStream fos = new FileOutputStream(target);
BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);
while ((size = zin.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, size);
}
bos.flush();
bos.close();
}

@Override
public String getApiName()
{
return "Ti.Compression";
}
public class CompressionModule extends KrollModule {


@Kroll.constant
static final int BEST_SPEED = Deflater.BEST_SPEED;
@Kroll.constant
static final int NO_COMPRESSION = Deflater.NO_COMPRESSION;
@Kroll.constant
static final int DEFAULT_COMPRESSION = Deflater.DEFAULT_COMPRESSION;

int level = -1;

public CompressionModule() {
super();
}

@Kroll.method
public String zip(Object[] args) {
level = -1;
// Check that our arguments are valid.
if (args.length == 3) {
HashMap options = (HashMap) args[2];
if (options.get("compression") != null) {
level = (int) options.get("compression");
}
} else if (args.length != 2) {
return "Invalid number of arguments provided!";
}



String rawZip = (String) args[0];
if (rawZip == null || rawZip.length() == 0) {
return "archiveFile was not specified or was empty!";
}
TiBaseFile zip = TiFileFactory.createTitaniumFile(rawZip, false);

Object[] rawFiles = (Object[]) args[1];
if (rawFiles == null || rawFiles.length == 0) {
return "fileArray was not specified or was empty!";
}
LinkedList<TiBaseFile> files = new LinkedList<TiBaseFile>();
for (Object rawFile : rawFiles) {
files.add(TiFileFactory.createTitaniumFile(rawFile.toString(), false));
}

// And then zip the files in to the archive.
try {
OutputStream fout = zip.getOutputStream();
ZipOutputStream zout = new ZipOutputStream(new BufferedOutputStream(fout));

zout.setLevel(level);

while (!files.isEmpty()) {
TiBaseFile file = files.remove();
if (!file.exists()) {
Util.e("Skipping over file, because it does not exist: " + file.nativePath());
} else {
ZipEntry ze = new ZipEntry(file.name());
zout.putNextEntry(ze);
writeInFile(file, zout);
zout.closeEntry();
}
}
zout.close();
return "success";
} catch (Exception e) {
Util.e("Hit exception while unzipping the archive!", e);
return e.toString();
}
}

@Kroll.method
public String unzip(Object[] args) {
// Check that our arguments are valid.
if (args.length != 3) {
return "Invalid number of arguments provided!";
}

String rawDest = (String) args[0];
if (rawDest == null || rawDest.length() == 0) {
return "destinationFolder was not specified or was empty!";
}
String destPath = TiFileFactory.createTitaniumFile(rawDest, false).getNativeFile().getAbsolutePath();

String rawZip = (String) args[1];
if (rawZip == null || rawZip.length() == 0) {
return "archiveFile was not specified or was empty!";
}
TiBaseFile zip = TiFileFactory.createTitaniumFile(rawZip, false);
if (!zip.exists())
return "archiveFile was not found at " + rawZip + "!";

Boolean overwrite = (Boolean) args[2];
if (overwrite == null) {
return "overwrite was not specified!";
}

// And then unzip the archive.
try {
InputStream fin = zip.getInputStream();
ZipInputStream zin = new ZipInputStream(new BufferedInputStream(fin));
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
String target = destPath + "/" + ze.getName();
if (ze.isDirectory()) {
ensureDirectoryExists(target);
} else {
File file = new File(target);
if (overwrite || !file.exists()) {
writeOutFile(file, target, zin);
}
}
}
zin.close();
fin.close();
return "success";
} catch (Exception e) {
Util.e("Hit exception while unzipping the archive!", e);
return e.toString();
}
}

public void ensureDirectoryExists(String target) {
File f = new File(target);
if (!f.isDirectory()) {
f.mkdirs();
}
}

private void writeInFile(TiBaseFile tifile, ZipOutputStream zout) throws IOException {
int size;
byte[] buffer = new byte[2048];
InputStream fos = tifile.getInputStream();
BufferedInputStream bos = new BufferedInputStream(fos, buffer.length);
while ((size = bos.read(buffer, 0, buffer.length)) != -1) {
zout.write(buffer, 0, size);
}
}

private void writeOutFile(File file, String target, ZipInputStream zin) throws IOException {
// Make sure the parent directory exists.
file.getParentFile().mkdirs();
// Write out the file.
int size;
byte[] buffer = new byte[2048];
FileOutputStream fos = new FileOutputStream(target);
BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);
while ((size = zin.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, size);
}
bos.flush();
bos.close();
}

@Override
public String getApiName() {
return "Ti.Compression";
}
}
Loading

0 comments on commit fde2abe

Please sign in to comment.