11
11
import java .io .IOException ;
12
12
import java .io .OutputStream ;
13
13
import java .util .ArrayList ;
14
+ import java .util .HashSet ;
14
15
import java .util .zip .Adler32 ;
15
16
import java .util .zip .CheckedOutputStream ;
16
17
import java .util .zip .ZipEntry ;
@@ -21,10 +22,11 @@ private void s(String s2) { // an alias to avoid typing so much!
21
22
Log .d (Util .myLogName , s2 );
22
23
}
23
24
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 >();
28
30
29
31
public FileZipper (OutputStream dest , ArrayList <UriInterpretation > inputUriInterpretations , ContentResolver contentResolver ) {
30
32
/*
@@ -121,6 +123,20 @@ void addFile(int BUFFER, ZipOutputStream out, byte[] data,
121
123
}
122
124
123
125
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 ){
124
140
/* Galery Sends uri.getPath() with values like /external/images/media/16458
125
141
* while urlFile.name returns IMG_20120427_120038.jpg
126
142
*
0 commit comments