use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project buck by facebook.
the class ProjectFilesystemTest method testCreateZipPreservesExecutablePermissions.
@Test
public void testCreateZipPreservesExecutablePermissions() throws IOException {
// Create a empty executable file.
Path exe = tmp.newFile("test.exe");
MoreFiles.makeExecutable(exe);
// Archive it into a zipfile using `ProjectFileSystem.createZip`.
Path zipFile = tmp.getRoot().resolve("test.zip");
filesystem.createZip(ImmutableList.of(exe), zipFile);
// permissions.
try (ZipFile zip = new ZipFile(zipFile.toFile())) {
Enumeration<ZipArchiveEntry> entries = zip.getEntries();
assertTrue(entries.hasMoreElements());
ZipArchiveEntry entry = entries.nextElement();
Set<PosixFilePermission> permissions = MorePosixFilePermissions.fromMode(entry.getExternalAttributes() >> 16);
assertTrue(permissions.contains(PosixFilePermission.OWNER_EXECUTE));
assertFalse(entries.hasMoreElements());
}
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project buck by facebook.
the class UnzipTest method testExtractZipFilePreservesExecutePermissionsAndModificationTime.
@Test
public void testExtractZipFilePreservesExecutePermissionsAndModificationTime() throws IOException {
// getFakeTime returs time with some non-zero millis. By doing division and multiplication by
// 1000 we get rid of that.
final long time = ZipConstants.getFakeTime() / 1000 * 1000;
// Create a simple zip archive using apache's commons-compress to store executable info.
try (ZipArchiveOutputStream zip = new ZipArchiveOutputStream(zipFile.toFile())) {
ZipArchiveEntry entry = new ZipArchiveEntry("test.exe");
entry.setUnixMode((int) MorePosixFilePermissions.toMode(PosixFilePermissions.fromString("r-x------")));
entry.setSize(DUMMY_FILE_CONTENTS.length);
entry.setMethod(ZipEntry.STORED);
entry.setTime(time);
zip.putArchiveEntry(entry);
zip.write(DUMMY_FILE_CONTENTS);
zip.closeArchiveEntry();
}
// Now run `Unzip.extractZipFile` on our test zip and verify that the file is executable.
Path extractFolder = tmpFolder.newFolder();
ImmutableList<Path> result = Unzip.extractZipFile(zipFile.toAbsolutePath(), extractFolder.toAbsolutePath(), Unzip.ExistingFileMode.OVERWRITE);
Path exe = extractFolder.toAbsolutePath().resolve("test.exe");
assertTrue(Files.exists(exe));
assertThat(Files.getLastModifiedTime(exe).toMillis(), Matchers.equalTo(time));
assertTrue(Files.isExecutable(exe));
assertEquals(ImmutableList.of(extractFolder.resolve("test.exe")), result);
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project AozoraEpub3 by hmdev.
the class ImageInfoReader method getImage.
/** ファイル名から画像を取得
* 拡張子変更等は外側で修正しておく
* ファイルシステムまたはZipファイルから指定されたファイル名の画像を取得
* @param srcImageFileName ファイル名 Zipならエントリ名
* ※先頭からシークされるので遅い?
* @throws RarException */
public BufferedImage getImage(String srcImageFileName) throws IOException, RarException {
if (this.isFile) {
File file = new File(this.srcParentPath + srcImageFileName);
if (!file.exists()) {
//拡張子修正
srcImageFileName = this.correctExt(srcImageFileName);
file = new File(this.srcParentPath + srcImageFileName);
if (!file.exists())
return null;
}
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file), 8192);
try {
return ImageUtils.readImage(srcImageFileName.substring(srcImageFileName.lastIndexOf('.') + 1).toLowerCase(), bis);
} finally {
bis.close();
}
} else {
if (this.srcFile.getName().endsWith(".rar")) {
InputStream is = null;
Archive archive = new Archive(srcFile);
try {
FileHeader fileHeader = archive.nextFileHeader();
while (fileHeader != null) {
if (!fileHeader.isDirectory()) {
String entryName = fileHeader.getFileNameW();
if (entryName.length() == 0)
entryName = fileHeader.getFileNameString();
entryName = entryName.replace('\\', '/');
if (srcImageFileName.equals(entryName)) {
is = archive.getInputStream(fileHeader);
return ImageUtils.readImage(srcImageFileName.substring(srcImageFileName.lastIndexOf('.') + 1).toLowerCase(), is);
}
}
fileHeader = archive.nextFileHeader();
}
} finally {
if (is != null)
is.close();
archive.close();
}
} else {
ZipFile zf = new ZipFile(this.srcFile, "MS932");
ZipArchiveEntry entry = zf.getEntry(srcImageFileName);
if (entry == null) {
srcImageFileName = this.correctExt(srcImageFileName);
entry = zf.getEntry(srcImageFileName);
if (entry == null)
return null;
}
InputStream is = zf.getInputStream(entry);
try {
return ImageUtils.readImage(srcImageFileName.substring(srcImageFileName.lastIndexOf('.') + 1).toLowerCase(), is);
} catch (Exception e) {
e.printStackTrace();
} finally {
is.close();
zf.close();
}
}
}
return null;
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project AozoraEpub3 by hmdev.
the class Epub3ImageWriter method startImageSection.
/** セクション開始.
* @throws IOException */
private void startImageSection(String srcImageFilePath) throws IOException {
this.sectionIndex++;
String sectionId = decimalFormat.format(this.sectionIndex);
//package.opf用にファイル名
SectionInfo sectionInfo = new SectionInfo(sectionId);
//画像専用指定
sectionInfo.setImagePage(true);
//画像サイズが横長なら幅に合わせる
ImageInfo imageInfo = this.imageInfoReader.getImageInfo(srcImageFilePath);
if (imageInfo != null) {
if ((double) imageInfo.getWidth() / imageInfo.getHeight() >= (double) this.dispW / this.dispH) {
if (this.rotateAngle != 0 && this.dispW < this.dispH && (double) imageInfo.getHeight() / imageInfo.getWidth() < (double) this.dispW / this.dispH) {
//縦長画面で横長
imageInfo.rotateAngle = this.rotateAngle;
if (this.imageSizeType != SectionInfo.IMAGE_SIZE_TYPE_AUTO)
sectionInfo.setImageFitH(true);
} else {
//高さでサイズ調整する場合は高さの%指定
if (this.imageSizeType == SectionInfo.IMAGE_SIZE_TYPE_HEIGHT)
sectionInfo.setImageHeight(((double) imageInfo.getHeight() / imageInfo.getWidth()) * ((double) this.dispW / this.dispH));
else if (this.imageSizeType == SectionInfo.IMAGE_SIZE_TYPE_ASPECT)
sectionInfo.setImageFitW(true);
}
} else {
if (this.rotateAngle != 0 && this.dispW > this.dispH && (double) imageInfo.getHeight() / imageInfo.getWidth() > (double) this.dispW / this.dispH) {
//横長画面で縦長
imageInfo.rotateAngle = this.rotateAngle;
//高さでサイズ調整する場合は高さの%指定
if (this.imageSizeType == SectionInfo.IMAGE_SIZE_TYPE_HEIGHT)
sectionInfo.setImageHeight(((double) imageInfo.getHeight() / imageInfo.getWidth()) * ((double) this.dispW / this.dispH));
else if (this.imageSizeType == SectionInfo.IMAGE_SIZE_TYPE_ASPECT)
sectionInfo.setImageFitW(true);
} else {
if (this.imageSizeType != SectionInfo.IMAGE_SIZE_TYPE_AUTO)
sectionInfo.setImageFitH(true);
}
}
}
this.sectionInfos.add(sectionInfo);
//目次追加
if (this.sectionIndex == 1 || this.sectionIndex % 5 == 0)
this.addChapter(null, "" + this.sectionIndex, 0);
super.zos.putArchiveEntry(new ZipArchiveEntry(OPS_PATH + XHTML_PATH + sectionId + ".xhtml"));
//ヘッダ出力
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(super.zos, "UTF-8"));
//出力開始するセクションに対応したSectionInfoを設定
this.velocityContext.put("sectionInfo", sectionInfo);
Velocity.getTemplate(this.templatePath + OPS_PATH + XHTML_PATH + XHTML_HEADER_VM).merge(this.velocityContext, bw);
bw.flush();
}
use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry in project AozoraEpub3 by hmdev.
the class Epub3Writer method writeArchiveImage.
/** アーカイブ内の画像を出力 */
void writeArchiveImage(String srcImageFileName, InputStream is) throws IOException {
//拡張子修正
srcImageFileName = imageInfoReader.correctExt(srcImageFileName);
ImageInfo imageInfo = imageInfoReader.getImageInfo(srcImageFileName);
//Zip内テキストの場合はidと出力ファイル名が登録されていなければ出力しない。
if (imageInfo != null) {
if (imageInfo.getId() != null) {
//回転チェック
if ((double) imageInfo.getWidth() / imageInfo.getHeight() >= (double) this.dispW / this.dispH) {
if (this.rotateAngle != 0 && this.dispW < this.dispH && (double) imageInfo.getHeight() / imageInfo.getWidth() < (double) this.dispW / this.dispH) {
//縦長画面で横長
imageInfo.rotateAngle = this.rotateAngle;
}
} else {
if (this.rotateAngle != 0 && this.dispW > this.dispH && (double) imageInfo.getHeight() / imageInfo.getWidth() > (double) this.dispW / this.dispH) {
//横長画面で縦長
imageInfo.rotateAngle = this.rotateAngle;
}
}
zos.putArchiveEntry(new ZipArchiveEntry(OPS_PATH + IMAGES_PATH + imageInfo.getOutFileName()));
//Zip,Rarからの直接読み込みは失敗するので一旦バイト配列にする
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtils.copy(new BufferedInputStream(is, 16384), baos);
byte[] bytes = baos.toByteArray();
baos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
this.writeImage(bais, zos, imageInfo);
bais.close();
zos.closeArchiveEntry();
}
if (this.canceled)
return;
if (this.jProgressBar != null)
this.jProgressBar.setValue(this.jProgressBar.getValue() + 10);
}
}
Aggregations