Delete Zip Folder in Android

How do I delete files programmatically on Android?

Why don't you test this with this code:

File fdelete = new File(uri.getPath());
if (fdelete.exists()) {
if (fdelete.delete()) {
System.out.println("file Deleted :" + uri.getPath());
} else {
System.out.println("file not Deleted :" + uri.getPath());
}
}

I think part of the problem is you never try to delete the file, you just keep creating a variable that has a method call.

So in your case you could try:

File file = new File(uri.getPath());
file.delete();
if(file.exists()){
file.getCanonicalFile().delete();
if(file.exists()){
getApplicationContext().deleteFile(file.getName());
}
}

However I think that's a little overkill.

You added a comment that you are using an external directory rather than a uri. So instead you should add something like:

String root = Environment.getExternalStorageDirectory().toString();
File file = new File(root + "/images/media/2918");

Then try to delete the file.

Delete a zip file after unzip in java

If file.delete() returns false, then my guess is that another process has the zip file open - or possibly even your own process.

  • Check that you've got the path correct, e.g. what does file.exists() return?
  • Check that you've got permission to delete the file as the user running your application
  • Check that you haven't got an open handle to the file within your code (e.g. have you just read from it and not closed the input stream?)
  • Check that you don't have the file opened in a desktop app

android unzip folder from zip file and read content from that folder

private void unzip(String src, String dest){

final int BUFFER_SIZE = 4096;

BufferedOutputStream bufferedOutputStream = null;
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(src);
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
ZipEntry zipEntry;

while ((zipEntry = zipInputStream.getNextEntry()) != null){

String zipEntryName = zipEntry.getName();

String name = dest.substring(dest.lastIndexOf("/")-1);

File FileName = new File(FolderName);
if (!FileName.isDirectory()) {
try {
if (FileName.mkdir()) {
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
}

File file = new File(FolderName+"/" +zipEntryName);

if (file.exists()){

} else {
if(zipEntry.isDirectory()){
file.mkdirs();
}else{
byte buffer[] = new byte[BUFFER_SIZE];
FileOutputStream fileOutputStream = new FileOutputStream(file);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream, BUFFER_SIZE);
int count;

while ((count = zipInputStream.read(buffer, 0, BUFFER_SIZE)) != -1) {
bufferedOutputStream.write(buffer, 0, count);
}

bufferedOutputStream.flush();
bufferedOutputStream.close();
}
}
}
zipInputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Try This code it's working for me.



Related Topics



Leave a reply



Submit