use of java.util.zip.ZipInputStream in project che by eclipse.
the class ZipArchiverTest method assertThatZipArchiveContainsAllEntries.
private void assertThatZipArchiveContainsAllEntries(InputStream in, Map<String, String> entries) throws Exception {
try (ZipInputStream zip = new ZipInputStream(in)) {
ZipEntry zipEntry;
while ((zipEntry = zip.getNextEntry()) != null) {
String name = zipEntry.getName();
assertTrue(String.format("Unexpected entry %s in zip", name), entries.containsKey(name));
if (!zipEntry.isDirectory()) {
String content = new String(ByteStreams.toByteArray(zip));
assertEquals(String.format("Invalid content of file %s", name), entries.get(name), content);
}
entries.remove(name);
zip.closeEntry();
}
}
assertTrue(String.format("Expected but were not found in zip %s", entries), entries.isEmpty());
}
use of java.util.zip.ZipInputStream in project che by eclipse.
the class ZipArchiver method extract.
@Override
public void extract(InputStream zipInput, boolean overwrite, int stripNumber) throws IOException, ForbiddenException, ConflictException, ServerException {
try (ZipInputStream zip = new ZipInputStream(ZipContent.of(zipInput).getContent())) {
InputStream notClosableInputStream = new NotClosableInputStream(zip);
ZipEntry zipEntry;
while ((zipEntry = zip.getNextEntry()) != null) {
VirtualFile extractFolder = folder;
Path relativePath = Path.of(zipEntry.getName());
if (stripNumber > 0) {
if (relativePath.length() <= stripNumber) {
continue;
}
relativePath = relativePath.subPath(stripNumber);
}
if (zipEntry.isDirectory()) {
if (!extractFolder.hasChild(relativePath)) {
extractFolder.createFolder(relativePath.toString());
}
continue;
}
if (relativePath.length() > 1) {
Path neededParentPath = relativePath.getParent();
VirtualFile neededParent = extractFolder.getChild(neededParentPath);
if (neededParent == null) {
neededParent = extractFolder.createFolder(neededParentPath.toString());
}
extractFolder = neededParent;
}
String fileName = relativePath.getName();
VirtualFile file = extractFolder.getChild(Path.of(fileName));
if (file == null) {
extractFolder.createFile(fileName, notClosableInputStream);
} else {
if (overwrite) {
file.updateContent(notClosableInputStream);
} else {
throw new ConflictException(String.format("File '%s' already exists", file.getPath()));
}
}
zip.closeEntry();
}
}
}
use of java.util.zip.ZipInputStream in project elasticsearch by elastic.
the class FileTestUtils method unzip.
/**
* Unzip a zip file to a destination directory. If the zip file does not exist, an IOException is thrown.
* If the destination directory does not exist, it will be created.
*
* @param zip zip file to unzip
* @param destDir directory to unzip the file to
* @param prefixToRemove the (optional) prefix in the zip file path to remove when writing to the destination directory
* @throws IOException if zip file does not exist, or there was an error reading from the zip file or
* writing to the destination directory
*/
public static void unzip(final Path zip, final Path destDir, @Nullable final String prefixToRemove) throws IOException {
if (Files.notExists(zip)) {
throw new IOException("[" + zip + "] zip file must exist");
}
Files.createDirectories(destDir);
try (ZipInputStream zipInput = new ZipInputStream(Files.newInputStream(zip))) {
ZipEntry entry;
while ((entry = zipInput.getNextEntry()) != null) {
final String entryPath;
if (prefixToRemove != null) {
if (entry.getName().startsWith(prefixToRemove)) {
entryPath = entry.getName().substring(prefixToRemove.length());
} else {
throw new IOException("prefix not found: " + prefixToRemove);
}
} else {
entryPath = entry.getName();
}
final Path path = Paths.get(destDir.toString(), entryPath);
if (entry.isDirectory()) {
Files.createDirectories(path);
} else {
Files.copy(zipInput, path);
}
zipInput.closeEntry();
}
}
}
use of java.util.zip.ZipInputStream in project MyDiary by erttyy8821.
the class ZipManager method unzip.
public void unzip(String backupZieFilePath, String location) throws IOException {
int size;
byte[] buffer = new byte[BUFFER_SIZE];
try {
if (!location.endsWith("/")) {
location += "/";
}
File f = new File(location);
if (!f.isDirectory()) {
f.mkdirs();
}
ZipInputStream zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(backupZieFilePath), BUFFER_SIZE));
try {
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
String path = location + ze.getName();
File unzipFile = new File(path);
if (ze.isDirectory()) {
if (!unzipFile.isDirectory()) {
unzipFile.mkdirs();
}
} else {
// check for and create parent directories if they don't exist
File parentDir = unzipFile.getParentFile();
if (null != parentDir) {
if (!parentDir.isDirectory()) {
parentDir.mkdirs();
}
}
// unzip the file
FileOutputStream out = new FileOutputStream(unzipFile, false);
BufferedOutputStream fout = new BufferedOutputStream(out, BUFFER_SIZE);
try {
while ((size = zin.read(buffer, 0, BUFFER_SIZE)) != -1) {
fout.write(buffer, 0, size);
}
zin.closeEntry();
} finally {
fout.flush();
fout.close();
}
}
}
} finally {
zin.close();
}
} catch (Exception e) {
Log.e(TAG, "Unzip exception", e);
}
}
use of java.util.zip.ZipInputStream in project buck by facebook.
the class RepackZipEntriesStep method execute.
@Override
public StepExecutionResult execute(ExecutionContext context) {
Path inputFile = filesystem.getPathForRelativePath(inputPath);
Path outputFile = filesystem.getPathForRelativePath(outputPath);
try (ZipInputStream in = new ZipInputStream(new BufferedInputStream(Files.newInputStream(inputFile)));
CustomZipOutputStream out = ZipOutputStreams.newOutputStream(outputFile)) {
for (ZipEntry entry = in.getNextEntry(); entry != null; entry = in.getNextEntry()) {
CustomZipEntry customEntry = new CustomZipEntry(entry);
if (entries.contains(customEntry.getName())) {
customEntry.setCompressionLevel(compressionLevel.getValue());
}
InputStream toUse;
// If we're using STORED files, we must pre-calculate the CRC.
if (customEntry.getMethod() == ZipEntry.STORED) {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
ByteStreams.copy(in, bos);
byte[] bytes = bos.toByteArray();
customEntry.setCrc(Hashing.crc32().hashBytes(bytes).padToLong());
customEntry.setSize(bytes.length);
customEntry.setCompressedSize(bytes.length);
toUse = new ByteArrayInputStream(bytes);
}
} else {
toUse = in;
}
out.putNextEntry(customEntry);
ByteStreams.copy(toUse, out);
out.closeEntry();
}
return StepExecutionResult.SUCCESS;
} catch (IOException e) {
context.logError(e, "Unable to repack zip");
return StepExecutionResult.ERROR;
}
}
Aggregations