简体   繁体   中英

FileNotFoundException (no such file or directory)

I'm writing an android app and I need to read several files from several folders and add them to several zip archives. I need to limit the max size of the archives to lets say 16mb. So at runtime while adding the files to the archive if the size of it exceeds 16 mb create another archive with the same size limit and so on. I'm using the following wrapper class:

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();
    }
}

and I use it like this:

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

but an instantiation of the ChunkedZippedOutputStream object I get this error:

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

I know I'm doing something wrong with the path input or the name but I can't figure it out what.

Also if the code snippet is not correct please tell me, I got it from here How to split a huge zip file into multiple volumes?

If there is a simpler solution to my problem please tell me. Thank you

The output directory doesn't exist. See File.mkdirs() for the solution.

This worked for me

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

Example:

for MyFile1, you can use new File("MyFile1.txt");
for MyFile2, you can use new File("./res/files/MyFile2.txt");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM