I have an error while I try to extract the files of a .zip that I have stored in my assets folder, the error appear because my file have the character ñ. The error throws when I try to get next entry: zipIs.getNextEntry()
private void loadzip(String folder, InputStream inputStream) throws IOException
{
ZipInputStream zipIs = new ZipInputStream(inputStream);
ZipEntry ze = null;
int i=0;
while ((ze = zipIs.getNextEntry()) != null) {
FileOutputStream fout = new FileOutputStream(folder +"/"+ ze.getName());
byte[] buffer = new byte[1024];
int length = 0;
while ((length = zipIs.read(buffer))>0) {
fout.write(buffer, 0, length);
}
zipIs.closeEntry();
fout.close();
}
zipIs.close();
}
SOLUTION
Using zip4j
private void loadZip(String zipFileName, String destination)
{
ZipFile zipFile = null;
List<FileHeader> headers = null;
try {
zipFile = new ZipFile(zipFileName);
headers = zipFile.getFileHeaders();
} catch (ZipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(headers != null)
{
for(int i=0;i<headers.size();i++)
{
try {
zipFile.extractFile(headers.get(i),destination);
} catch (ZipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
.................................................................................................................................
If you want to load from assets (my case), you have to move your zip to sdcard folder and after that extract it:
private ArrayList<String> moveZipsFromAssets(String[] zipsAssets, String destination)
{
ArrayList<String> zips = new ArrayList<String>();
for(int i=0;i<zipsAssets.length;i++)
{
InputStream inputStream = getInputStream(zipsAssets[i]);
Log.d(tag, ""+zipsAssets[i]);
File file = new File(destination+"/"+zipsAssets[i]);
try {
FileOutputStream outputStream = new FileOutputStream(file);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
zips.add(destination+"/"+zipsAssets[i]);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return zips;
}
example use (we have a folder assets/myZipsFolder/ where are all the zips): `ArrayList
zipsExternos = moveZipsFromAssets(getAssets().list("myZipsFolder"),
Environment.getExternalStorageDirectory()+"/myZipsFolder");
//and load the zips:
for(int i=0;i<zipsExternos.size();i++)
loadZip(zipsExternos.get(i),Environment.getExternalStorageDirectory()+"/myZipsFolder");