0
public static void writeFile(String theFileName, String theFilePath)
{
    try {
        File currentFile = new File("plugins/mcMMO/Resources/"+theFilePath+theFileName);
        //System.out.println(theFileName);
        @SuppressWarnings("static-access")
        JarFile jar = new JarFile(plugin.mcmmo);
        JarEntry entry = jar.getJarEntry("resources/"+theFileName);
        InputStream is = jar.getInputStream(entry);
        byte[] buf = new byte[(int)entry.getSize()];
        is.read(buf, 0, buf.length);
        FileOutputStream os = new FileOutputStream(currentFile);
        os.write(buf);
        os.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

さて、私のプログラムでは、プログラムのJar内にさまざまなリソースが保持されています。プログラムが実行されると、この関数に渡された特定のファイルがユーザーのコンピューターのHDDに書き込まれます。すべてが書き込まれますが、画像だけが 100% 正しく出力されます。サウンドファイルはそれほど幸運ではありません。

基本的に、サウンドを正しく書き込むことができません。ファイル サイズは正しいのですが、完全な長さのオーディオではなく、ほんの一瞬のオーディオしか含まれていません。ここで何か不足していますか?私はすべて正しいことをしたように見えますが、それが本当ならここに投稿することはありません.

この問題をグーグルで調べようと最善を尽くしましたが、失敗しました。

これが機能しない理由についての推測は驚くべきものです!! :)

4

1 に答える 1

0

JarEntryextendsとして、 -1 を返すためZipEntry、メソッドに依存しないことをお勧めします。ドキュメントZipEntry.getSize()を参照してください。

さらに、ストリームを読み取るときにバッファリングを利用することは、一般的にはるかに一般的です。あなたの例では、すべてをバイト配列内に配置しているため、大きなファイルの場合はOutOfMemoryError.

テストするコードは次のとおりです。

public static void writeFile(String theFileName, String theFilePath)
{
    try {
        File currentFile = new File("plugins/mcMMO/Resources/"+theFilePath+theFileName);
        @SuppressWarnings("static-access")
        JarFile jar = new JarFile(plugin.mcmmo);
        JarEntry entry = jar.getJarEntry("resources/"+theFileName);
        InputStream is = jar.getInputStream(entry);
        byte[] buf = new byte[2048];
        int nbRead;
        OutputStream os = new BufferedOutputStream(new FileOutputStream(currentFile));
        while((nbRead = is.read(buf)) != -1) {
            os.write(buf, 0, nbRead);
        }
        os.flush();
        os.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
于 2011-09-06T06:42:02.703 に答える