-
Notifications
You must be signed in to change notification settings - Fork 239
Improved GPX Sharing: Share as ZIP #501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
76ca5ac
feat: Enable sharing tracks as a ZIP file if they have associated files
Andyporras ebb8bff
Translate comments to English and update specific imports
Andyporras 0b9e4b6
zipCacheFiles now returns a ZIP and sharing only allows ZIP files
Andyporras b27f2de
Fix duplicate filename issue when sharing traces
Andyporras 873e355
Code refactor in share functionality
jamescr 9d32da8
Fix: create zip without multimedia
Andyporras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
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) { | ||
jamescr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
String name = fileGPX.getName(); | ||
File zipFile = new File(context.getCacheDir(), | ||
name.substring(0, name.length() - 3) + 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; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* 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(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.