Skip to content

Commit ddc9927

Browse files
committed
v2.0.9: bugfix: workaround for creating zips containing files with the exact name
1 parent fd6ce0a commit ddc9927

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

ShareViaHttp/app/src/main/java/com/MarcosDiez/shareviahttp/FileZipper.java

+20-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.io.IOException;
1212
import java.io.OutputStream;
1313
import java.util.ArrayList;
14+
import java.util.HashSet;
1415
import java.util.zip.Adler32;
1516
import java.util.zip.CheckedOutputStream;
1617
import java.util.zip.ZipEntry;
@@ -21,10 +22,11 @@ private void s(String s2) { // an alias to avoid typing so much!
2122
Log.d(Util.myLogName, s2);
2223
}
2324

24-
OutputStream dest;
25-
ArrayList<UriInterpretation> inputUriInterpretations;
26-
Boolean atLeastOneDirectory = false;
27-
ContentResolver contentResolver;
25+
private OutputStream dest;
26+
private ArrayList<UriInterpretation> inputUriInterpretations;
27+
private Boolean atLeastOneDirectory = false;
28+
private ContentResolver contentResolver;
29+
private HashSet<String> fileNamesAlreadyUsed = new HashSet<String>();
2830

2931
public FileZipper(OutputStream dest, ArrayList<UriInterpretation> inputUriInterpretations, ContentResolver contentResolver) {
3032
/*
@@ -121,6 +123,20 @@ void addFile(int BUFFER, ZipOutputStream out, byte[] data,
121123
}
122124

123125
String getFileName(UriInterpretation uriFile) {
126+
/* The Android Galary sends us two files with the same name, we must make them unique or we get a
127+
* "java.util.zip.ZipException: duplicate entry: x.gif" exception
128+
*/
129+
String fileName = getFileNameHelper(uriFile);
130+
while(!fileNamesAlreadyUsed.add(fileName)){
131+
// fileNamesAlreadyUsed.add returns TRUE if the file was added to the HashSet
132+
// false it it was already there.
133+
// in this case we must change the filename, to keep it unique
134+
fileName = "_" + fileName;
135+
}
136+
return fileName;
137+
}
138+
139+
private String getFileNameHelper(UriInterpretation uriFile){
124140
/* Galery Sends uri.getPath() with values like /external/images/media/16458
125141
* while urlFile.name returns IMG_20120427_120038.jpg
126142
*

0 commit comments

Comments
 (0)