I am trying to count the number of files in an archive. The problem is that my code counts all objects, including folders (for example, if I have a complex directory, but only one file in it, I cannot check my archive). I am using the size () method.
import java.nio.file.Path;
import javax.enterprise.context.Dependent;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import ru.cntp.eupp.roster.Notification;
import java.util.ArrayList;
import java.util.zip.ZipFile;
import java.util.List;
import java.util.Enumeration;
@Dependent
public class ZipValidator {
public void validate(Path pathToFile) throws IOException {
ZipFile zipFile = new ZipFile(pathToFile.toFile());
if (zipFile.size() != 1 && zipFile.size() != 2) {
throw new InvalidZipException("The number of files in archive is more than 2");
}
}
}
source
share