簡體   English   中英

FileNotFoundException(沒有這樣的文件或目錄)

[英]FileNotFoundException (no such file or directory)

我正在編寫一個 android 應用程序,我需要從幾個文件夾中讀取幾個文件並將它們添加到幾個 zip 檔案中。 我需要將檔案的最大大小限制為 16mb。 因此,在運行時將文件添加到存檔時,如果它的大小超過 16 mb,則創建另一個具有相同大小限制的存檔,依此類推。 我正在使用以下包裝類:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ChunkedZippedOutputStream {
    private ZipOutputStream zipOutputStream;

    private String path;

    private String name;

    private long currentSize;

    private int currentChunkIndex;

    private final long MAX_FILE_SIZE = 16 * 1000 * 1024; // 16mb limit

    private final String PART_POSTFIX = ".part";

    private final String FILE_EXTENSION = ".zip";

    public ChunkedZippedOutputStream(String path, String name) throws FileNotFoundException {
        this.path = path;
        this.name = name;
        constructNewStream();
    }

    public void addEntry(ZipEntry entry) throws IOException {
        long entrySize = entry.getCompressedSize();
        if ((currentSize + entrySize) > MAX_FILE_SIZE) {
            closeStream();
            constructNewStream();
        } else {
            currentSize += entrySize;
            zipOutputStream.putNextEntry(entry);
        }
    }

    private void closeStream() throws IOException {
        zipOutputStream.close();
    }

    private void constructNewStream() throws FileNotFoundException {
        zipOutputStream = new ZipOutputStream(new FileOutputStream(new File(path, constructCurrentPartName())));
        currentChunkIndex++;
        currentSize = 0;
    }

    private String constructCurrentPartName() {
        // This will give names is the form of <file_name>.part.0.zip, <file_name>.part.1.zip, etc.
        StringBuilder partNameBuilder = new StringBuilder(name);
        partNameBuilder.append(PART_POSTFIX);
        partNameBuilder.append(currentChunkIndex);
        partNameBuilder.append(FILE_EXTENSION);
        return partNameBuilder.toString();
    }
}

我像這樣使用它:

String zipPath = Environment.getExternalStorageDirectory() + "/MyApp/MyFolder/Zip/";
String zipName = "MyZipFle";
ChunkedZippedOutputStream zippedOutputStream = new ChunkedZippedOutputStream(zipPath, zipName);
....
zippedOutputStream.addEntry(new ZipEntry("ZipEntry" + i));

但是在 ChunkedZippedOutputStream 對象的一個​​實例化我得到這個錯誤:

  java.io.FileNotFoundException: /mnt/sdcard/MyApp/MyFolder/Zip/MyZipFle.part0.zip (No such file or directory)

我知道我的路徑輸入或名稱有問題,但我不知道是什么。

另外,如果代碼片段不正確,請告訴我,我從這里得到它如何將一個巨大的 zip 文件拆分為多個卷?

如果我的問題有更簡單的解決方案,請告訴我。 謝謝

輸出目錄不存在。 有關解決方案,請參閱File.mkdirs()

這對我有用

Project
|->src
|   |-->MyClass.java
|   |-->MyFile1.txt
|->res
   |->files
     |-->MyFile2.txt

例子:

對於 MyFile1,您可以使用 new File("MyFile1.txt");
對於 MyFile2,您可以使用 new File("./res/files/MyFile2.txt");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM