2017-01-15 6 views
-1

アンドロイドでzipファイルを解凍します。それは他の郵便ファイルのためにうまくいきますが、私はsdcardの任意のパスに抽出することができない1つのzipファイルを持っています。 このエラーが表示されます。/storage/emulated/0/AndroidAPP/addon_data/plugin.audio.mp3streams/artist_icons/David Bowie.jpg:オープンに失敗しました:ENOENT(そのようなファイルやディレクトリはありません)

/storage/emulated/0/AndroidTVAPP/addon_data/plugin.audio.mp3streams/artist_icons/David Bowie.jpg: open failed: ENOENT (No such file or directory) 

解凍ファイルJavaクラス

public class UnzipUtility 
{ 
/** 
* Size of the buffer to read/write data 
*/ 
private static final int BUFFER_SIZE = 4096; 
/** 
* Extracts a zip file specified by the zipFilePath to a directory specified by 
* destDirectory (will be created if does not exists) 
* @param zipFilePath 
* @param destDirectory 
* @throws IOException 
*/ 
public void unzip(String zipFilePath, String destDirectory) throws IOException 
{ 
    File destDir = new File(destDirectory); 
    if (!destDir.exists()) 
    { 
     destDir.mkdir(); 
    } 
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)); 
    ZipEntry entry = zipIn.getNextEntry(); 
    // iterates over entries in the zip file 
    while (entry != null) 
    { 



     String filePath = destDirectory + File.separator + entry.getName(); 

     String filePath2 = File.separator + entry.getName();// filePath; 

     if (null != filePath2 && filePath2.length() > 0) 
     { 
      int endIndex = filePath2.lastIndexOf("/"); 
      if (endIndex != -1) 
      { 
       filePath2 = filePath2.substring(0, endIndex); // not forgot to put check if(endIndex != -1) 

       Log.e("File path=",""+filePath2); 

       File root = android.os.Environment.getExternalStorageDirectory(); 
       File dir = new File(root.getAbsolutePath() + "/AndroidTVAPP"+File.separator+filePath2); 
        if(dir.exists() == false){ 
         dir.mkdirs(); 
        } 



      } 
     } 


     if (!entry.isDirectory()) { 
      // if the entry is a file, extracts it 
      extractFile(zipIn, filePath); 
     } else { 
      // if the entry is a directory, make the directory 
      File dir = new File(filePath); 
      dir.mkdir(); 
     } 
     zipIn.closeEntry(); 
     entry = zipIn.getNextEntry(); 
    } 
    zipIn.close(); 
} 
/** 
* Extracts a zip entry (file entry) 
* @param zipIn 
* @param filePath 
* @throws IOException 
*/ 
private void extractFile(ZipInputStream zipIn, String filePath) throws IOException 
{ 
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); 
    byte[] bytesIn = new byte[BUFFER_SIZE]; 
    int read = 0; 
    while ((read = zipIn.read(bytesIn)) != -1) { 
     bos.write(bytesIn, 0, read); 
    } 
    bos.close(); 
} 

}

+0

ファイル名のスペースでなければなりません。あなたはそれをエンコードしようとしましたか? –

+0

@KNeerajLalスペースがあるかもしれません.zipファイルは約45MBであり、たくさんのファイルがあるので、ここでunzipクラスをアップロードすれば、それを見てわかりやすくすることができます –

答えて

1

あなたは、ファイル名に空白があることを特定するエスケープ文字を使用する必要があります。

これを試してみてください

String path = "/storage/emulated/0/AndroidTVAPP/addon_data/plugin.audio.mp3streams/artist_icons/"; 
String fullPath= path + "David Bowie.jpg"; 
fullPath = fullPath.replace(" ", "\\ "); 

それとも、Uri.parse(path)を使用することができます。

public void unzip(String zipFilePath, String destDirectory) throws IOException { 
    ... 
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(Uri.parse(zipFilePath))); 
    ... 
} 
+0

最初に[ディレクトリを作成する必要があります](http://docs.oracle.com/javase/tutorial/essential/io/dirs.html)を参照してください。そして、[ファイルを移動する]必要があります(http://docs.oracle.com/javase/tutorial/essential/io/move.html)。 –

+0

私はコードを修正しましたが、上記のコードが更新されたディレクトリを作成する行をいくつか入れましたが、うまくいきました。ファイルを移動する前にディレクトリを作成する必要があります –

関連する問題

 関連する問題