use of java.util.zip.ZipEntry 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.ZipEntry 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.ZipEntry in project MyDiary by erttyy8821.
the class ZipManager method zipBackupJsonFile.
private void zipBackupJsonFile(String backupJsonFilePath, ZipOutputStream out) throws IOException {
byte[] data = new byte[BUFFER_SIZE];
FileInputStream fi = new FileInputStream(backupJsonFilePath);
BufferedInputStream jsonFileOrigin = new BufferedInputStream(fi, BUFFER_SIZE);
ZipEntry entry = new ZipEntry(BackupManager.BACKUP_JSON_FILE_NAME);
out.putNextEntry(entry);
int count;
while ((count = jsonFileOrigin.read(data, 0, BUFFER_SIZE)) != -1) {
out.write(data, 0, count);
}
}
use of java.util.zip.ZipEntry in project buck by facebook.
the class AgentUtil method getJarSignature.
public static String getJarSignature(String packagePath) throws IOException {
Pattern signatureFilePattern = Pattern.compile("META-INF/[A-Z]+\\.SF");
ZipFile packageZip = null;
try {
packageZip = new ZipFile(packagePath);
// For each file in the zip.
for (ZipEntry entry : Collections.list(packageZip.entries())) {
// Ignore non-signature files.
if (!signatureFilePattern.matcher(entry.getName()).matches()) {
continue;
}
BufferedReader sigContents = null;
try {
sigContents = new BufferedReader(new InputStreamReader(packageZip.getInputStream(entry)));
// For each line in the signature file.
while (true) {
String line = sigContents.readLine();
if (line == null || line.equals("")) {
throw new IllegalArgumentException("Failed to find manifest digest in " + entry.getName());
}
String prefix = "SHA1-Digest-Manifest: ";
if (line.startsWith(prefix)) {
return line.substring(prefix.length());
}
}
} finally {
if (sigContents != null) {
sigContents.close();
}
}
}
} finally {
if (packageZip != null) {
packageZip.close();
}
}
throw new IllegalArgumentException("Failed to find signature file.");
}
use of java.util.zip.ZipEntry in project buck by facebook.
the class DalvikAwareOutputStreamHelper method putEntry.
@Override
public void putEntry(FileLike fileLike) throws IOException {
String name = fileLike.getRelativePath();
// proguard seems to handle merging multiple -injars into a single -outjar.
if (!containsEntry(fileLike)) {
entryNames.add(name);
outStream.putNextEntry(new ZipEntry(name));
try (InputStream in = fileLike.getInput()) {
ByteStreams.copy(in, outStream);
}
// Make sure FileLike#getSize didn't lie (or we forgot to call canPutEntry).
DalvikStatsTool.Stats stats = dalvikStatsCache.getStats(fileLike);
Preconditions.checkState(!isEntryTooBig(fileLike), "Putting entry %s (%s) exceeded maximum size of %s", name, stats.estimatedLinearAllocSize, linearAllocLimit);
currentLinearAllocSize += stats.estimatedLinearAllocSize;
currentMethodReferences.addAll(stats.methodReferences);
currentFieldReferences.addAll(stats.fieldReferences);
String report = String.format("%d %d %s\n", stats.estimatedLinearAllocSize, stats.methodReferences.size(), name);
reportFileWriter.append(report);
}
}
Aggregations