저는 Android 앱을 작성 중이므로 여러 폴더에서 여러 파일을 읽고 여러 개의 zip 아카이브에 추가해야합니다. 나는 아카이브의 최대 크기를 16MB로 제한 할 필요가있다. 따라서 런타임시 파일 크기가 16MB를 초과하면 아카이브에 파일을 추가하는 동안 크기 제한이 같은 다른 아카이브를 생성하는 식으로 진행됩니다. 나는 다음과 같은 래퍼 클래스 사용하고 있습니다 :FileNotFoundException (해당 파일이나 디렉토리가 없음)
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();
}
}
및 I는 다음과 같이 사용 :
java.io.FileNotFoundException: /mnt/sdcard/MyApp/MyFolder/Zip/MyZipFle.part0.zip (No such file or directory)
I :
String zipPath = Environment.getExternalStorageDirectory() + "/MyApp/MyFolder/Zip/";
String zipName = "MyZipFle";
ChunkedZippedOutputStream zippedOutputStream = new ChunkedZippedOutputStream(zipPath, zipName);
....
zippedOutputStream.addEntry(new ZipEntry("ZipEntry" + i));
하지만 ChunkedZippedOutputStream 객체의 인스턴스화 나는이 오류 경로 입력 또는 이름에 문제가 있음을 알고 있지만 무엇인지 파악할 수는 없습니다.
또한 코드가 올바르지 않습니다 경우 내 문제에 대한 간단한 해결책이 제발 말해이 있다면 내가 How to split a huge zip file into multiple volumes?
여기에서 그것을 가지고, 저에게 알려주십시오. 감사합니다
맞습니다! 고맙습니다 – androidu