Reputation: 51
I have a program which downloads a bunch of documents from a remote server and after, bundles them up in a .zip file using ZipOutputStream.
The problem is that when I download the files, there could be more than one file with the same file name. So, when I try to put an entry with the same file name which is in already ZipOutputStream, it will throw an exception "duplicate entry".
Is there a way, I can check for duplicate entry, before adding the zip entry to ZipOutputStream? so I can rename the duplicate file?
Please advise...
Upvotes: 1
Views: 4971
Reputation: 27880
You could achieve that by adding each filename you add to an efficient data structure such as a HashSet
and check if the name is already there. The add
method, which returns true
if the element was not already in the Set
, and false
otherwise.
Set<String> addedNames = new HashSet<String>();
// Start processing of the file set
for (String fileName : fileNames) {
if (addedNames.add(fileName) {
// Process file
}
else {
throw new DuplicateException(fileName);
}
}
Upvotes: 1
Reputation: 1248
Why not registering all of your filenames in a sorted array/map/hash, and check all of them before adding your new file ?
Upvotes: 1