-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathZipHelper.java
More file actions
119 lines (98 loc) · 3.82 KB
/
Copy pathZipHelper.java
File metadata and controls
119 lines (98 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package net.osmtracker.gpx;
import android.content.Context;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import net.osmtracker.db.DataHelper;
public class ZipHelper {
private static final String TAG = "ZipHelper";
/**
* Compresses all files associated with a track into a ZIP file.
*
* @param context Application context.
* @param trackId Track ID.
* @param fileGPX GPX file.
* @return The created ZIP file or null if an error occurred.
*/
public static File zipCacheFiles(Context context, long trackId, File fileGPX) {
String name = fileGPX.getName();
File zipFile = new File(context.getCacheDir(),
name.substring(0, name.length() - 4) + DataHelper.EXTENSION_ZIP);
File traceFilesDirectory = DataHelper.getTrackDirectory(trackId, context);
try (FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos)) {
// Add gpx file
addFileToZip(fileGPX, zos);
if(!traceFilesDirectory.exists()){
return zipFile;
}
for (File multimediaFile : Objects.requireNonNull(traceFilesDirectory.listFiles())) {
if (!multimediaFile.isDirectory()) { // Avoid adding empty folders
// only add files that are not .zip files
if (!multimediaFile.getName().endsWith(DataHelper.EXTENSION_ZIP)) {
addFileToZip(multimediaFile, zos);
} else {
Log.d(TAG, "Multimedia file: " + multimediaFile.getAbsolutePath() + " ignored. ");
}
} else {
Log.d(TAG, "Folder " + multimediaFile.getAbsolutePath() + " ignored. ");
}
}
Log.d(TAG, "ZIP file created: " + zipFile.getAbsolutePath());
return zipFile;
} catch (IOException e) {
Log.e(TAG, "Error creating ZIP file", e);
return null;
}
}
/**
* Compresses track into a ZIP file.
*
* @param context Application context.
* @param trackId Track ID.
* @param fileGPX GPX file.
* @return The created ZIP file or null if an error occurred.
*/
public static File zipGPXFile(Context context, long trackId, File fileGPX) {
String name = fileGPX.getName();
File zipFile = new File(context.getCacheDir(),
name.substring(0, name.length() - 4) + DataHelper.EXTENSION_ZIP);
try (FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos)) {
// Add gpx file
addFileToZip(fileGPX, zos);
}
catch (IOException e) {
Log.e(TAG, "Error creating ZIP file", e);
return null;
}
return zipFile;
}
/**
* Adds a file to the ZIP archive.
*
* @param file The file to add.
* @param zos The ZipOutputStream to which the file will be added.
*/
private static void addFileToZip(File file, ZipOutputStream zos) throws IOException {
if (!file.exists()) {
Log.e(TAG, "File does not exist: " + file.getAbsolutePath());
return;
}
try (FileInputStream fis = new FileInputStream(file)) {
ZipEntry zipEntry = new ZipEntry(file.getName());
zos.putNextEntry(zipEntry);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
}
}
}