本文整理汇总了Java中org.apache.commons.compress.archivers.zip.ZipArchiveEntry.setMethod方法的典型用法代码示例。如果您正苦于以下问题:Java ZipArchiveEntry.setMethod方法的具体用法?Java ZipArchiveEntry.setMethod怎么用?Java ZipArchiveEntry.setMethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.compress.archivers.zip.ZipArchiveEntry
的用法示例。
在下文中一共展示了ZipArchiveEntry.setMethod方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: entryToNewName
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; //导入方法依赖的package包/类
/**
* Entry to new name.
*
* @param source the source
* @param name the name
* @return the zip archive entry
* @throws ZipException the zip exception
*/
public static ZipArchiveEntry entryToNewName(ZipArchiveEntry source, String name) throws ZipException {
if (source.getName().equals(name))
return new ZipArchiveEntry(source);
ZipArchiveEntry ret = new ZipArchiveEntry(name);
byte[] extra = source.getExtra();
if (extra != null) {
ret.setExtraFields(ExtraFieldUtils.parse(extra, true, ExtraFieldUtils.UnparseableExtraField.READ));
} else {
ret.setExtra(ExtraFieldUtils.mergeLocalFileDataData(source.getExtraFields(true)));
}
ret.setInternalAttributes(source.getInternalAttributes());
ret.setExternalAttributes(source.getExternalAttributes());
ret.setExtraFields(source.getExtraFields(true));
ret.setCrc(source.getCrc());
ret.setMethod(source.getMethod());
ret.setSize(source.getSize());
return ret;
}
示例2: createArchiveEntry
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; //导入方法依赖的package包/类
@Override
public ArchiveEntry createArchiveEntry(String targetPath, long targetSize, byte[] targetBytes) {
ZipArchiveEntry zipEntry = new ZipArchiveEntry(targetPath);
zipEntry.setSize(targetSize);
zipEntry.setMethod(ZipEntry.STORED);
if (targetBytes != null) {
zipEntry.setCrc(crc32Checksum(targetBytes));
}
return zipEntry;
}
示例3: testExtractZipFilePreservesExecutePermissions
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; //导入方法依赖的package包/类
@Test
public void testExtractZipFilePreservesExecutePermissions() throws IOException {
// Create a simple zip archive using apache's commons-compress to store executable info.
try (ZipArchiveOutputStream zip = new ZipArchiveOutputStream(zipFile)) {
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);
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.
File extractFolder = tmpFolder.newFolder();
ImmutableList<Path> result = Unzip.extractZipFile(
zipFile.toPath().toAbsolutePath(),
extractFolder.toPath().toAbsolutePath(),
false);
File exe = new File(extractFolder.getAbsoluteFile() + "/test.exe");
assertTrue(exe.exists());
assertTrue(exe.canExecute());
assertEquals(
ImmutableList.of(
extractFolder.toPath().resolve("test.exe")),
result);
}
示例4: testExtractZipFilePreservesExecutePermissionsAndModificationTime
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; //导入方法依赖的package包/类
@Test
public void testExtractZipFilePreservesExecutePermissionsAndModificationTime()
throws InterruptedException, 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 =
ArchiveFormat.ZIP
.getUnarchiver()
.extractArchive(
new DefaultProjectFilesystemFactory(),
zipFile.toAbsolutePath(),
extractFolder.toAbsolutePath(),
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);
}
示例5: testExtractSymlink
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; //导入方法依赖的package包/类
@Test
public void testExtractSymlink() throws InterruptedException, IOException {
assumeThat(Platform.detect(), Matchers.is(Matchers.not(Platform.WINDOWS)));
// 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("link.txt");
entry.setUnixMode((int) MoreFiles.S_IFLNK);
String target = "target.txt";
entry.setSize(target.getBytes(Charsets.UTF_8).length);
entry.setMethod(ZipEntry.STORED);
zip.putArchiveEntry(entry);
zip.write(target.getBytes(Charsets.UTF_8));
zip.closeArchiveEntry();
}
Path extractFolder = tmpFolder.newFolder();
ArchiveFormat.ZIP
.getUnarchiver()
.extractArchive(
new DefaultProjectFilesystemFactory(),
zipFile.toAbsolutePath(),
extractFolder.toAbsolutePath(),
ExistingFileMode.OVERWRITE);
Path link = extractFolder.toAbsolutePath().resolve("link.txt");
assertTrue(Files.isSymbolicLink(link));
assertThat(Files.readSymbolicLink(link).toString(), Matchers.equalTo("target.txt"));
}
示例6: testExtractBrokenSymlinkWithOwnerExecutePermissions
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; //导入方法依赖的package包/类
@Test
public void testExtractBrokenSymlinkWithOwnerExecutePermissions()
throws InterruptedException, IOException {
assumeThat(Platform.detect(), Matchers.is(Matchers.not(Platform.WINDOWS)));
// 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("link.txt");
entry.setUnixMode((int) MoreFiles.S_IFLNK);
String target = "target.txt";
entry.setSize(target.getBytes(Charsets.UTF_8).length);
entry.setMethod(ZipEntry.STORED);
// Mark the file as being executable.
Set<PosixFilePermission> filePermissions = ImmutableSet.of(PosixFilePermission.OWNER_EXECUTE);
long externalAttributes =
entry.getExternalAttributes() + (MorePosixFilePermissions.toMode(filePermissions) << 16);
entry.setExternalAttributes(externalAttributes);
zip.putArchiveEntry(entry);
zip.write(target.getBytes(Charsets.UTF_8));
zip.closeArchiveEntry();
}
Path extractFolder = tmpFolder.newFolder();
ArchiveFormat.ZIP
.getUnarchiver()
.extractArchive(
new DefaultProjectFilesystemFactory(),
zipFile.toAbsolutePath(),
extractFolder.toAbsolutePath(),
ExistingFileMode.OVERWRITE);
Path link = extractFolder.toAbsolutePath().resolve("link.txt");
assertTrue(Files.isSymbolicLink(link));
assertThat(Files.readSymbolicLink(link).toString(), Matchers.equalTo("target.txt"));
}
示例7: makeEntry
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; //导入方法依赖的package包/类
private ZipArchiveEntry makeEntry(String lpath) {
ZipArchiveEntry entry = new ZipArchiveEntry(lpath);
entry.setMethod(ZipArchiveEntry.STORED);
return entry;
}
示例8: isVulnerable
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; //导入方法依赖的package包/类
public boolean isVulnerable(Context context) throws Exception {
ByteArrayOutputStream fileNameFunz = new ByteArrayOutputStream();
ModdedZipArchiveOutputStream zaos = new ModdedZipArchiveOutputStream(fileNameFunz);
ZipArchiveEntry zae = new ZipArchiveEntry("test_file");
byte [] originalData = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".getBytes();
byte [] modifiedData = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB".getBytes();
zae.setMethod(ZipEntry.STORED);
zae.setSize(originalData.length);
CRC32 checker = new CRC32();
checker.update(originalData);
zae.setCrc(checker.getValue());
zaos.putArchiveEntry(zae, originalData);
zaos.write(originalData);
zaos.closeArchiveEntry();
zaos.writeRaw(modifiedData, 0, modifiedData.length);
for(int i = 0; i < originalData.length - modifiedData.length; i++){
zaos.writeRaw(new byte[]{0}, 0, 1);
}
zaos.flush();
List<ZipArchiveEntry> entries = new ArrayList<>();
entries.add(zae);
zaos.finish(entries, new ArrayList<ZipArchiveEntry>());
byte [] testZip = fileNameFunz.toByteArray();
// write the result to a file
File outputDir = context.getCacheDir();
File badZip = File.createTempFile("prefix", "extension", outputDir);
badZip.deleteOnExit();
FileOutputStream outstream = new FileOutputStream(badZip);
outstream.write(testZip);
outstream.close();
// see if we can still handle it
ZipFile bad = new ZipFile(badZip);
if(bad.size() != 1)
throw new ZipException("Unexpected number of entries");
ZipEntry ze = bad.entries().nextElement();
DataInputStream dis = new DataInputStream(bad.getInputStream(ze));
byte [] buf = new byte[(int)ze.getSize()];
dis.readFully(buf);
bad.close();
return new String(buf).startsWith("AAAAAAAAAAAAAAA");
}
示例9: testStripsPrefixAndIgnoresSiblings
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; //导入方法依赖的package包/类
@Test
public void testStripsPrefixAndIgnoresSiblings() throws IOException, InterruptedException {
byte[] bazDotSh = "echo \"baz.sh\"\n".getBytes(Charsets.UTF_8);
try (ZipArchiveOutputStream zip = new ZipArchiveOutputStream(zipFile.toFile())) {
zip.putArchiveEntry(new ZipArchiveEntry("foo"));
zip.closeArchiveEntry();
zip.putArchiveEntry(new ZipArchiveEntry("foo/bar/baz.txt"));
zip.write(DUMMY_FILE_CONTENTS, 0, DUMMY_FILE_CONTENTS.length);
zip.closeArchiveEntry();
ZipArchiveEntry exeEntry = new ZipArchiveEntry("foo/bar/baz.sh");
exeEntry.setUnixMode(
(int) MorePosixFilePermissions.toMode(PosixFilePermissions.fromString("r-x------")));
exeEntry.setMethod(ZipEntry.STORED);
exeEntry.setSize(bazDotSh.length);
zip.putArchiveEntry(exeEntry);
zip.write(bazDotSh);
zip.closeArchiveEntry();
zip.putArchiveEntry(new ZipArchiveEntry("sibling"));
zip.closeArchiveEntry();
zip.putArchiveEntry(new ZipArchiveEntry("sibling/some/dir/and/file.txt"));
zip.write(DUMMY_FILE_CONTENTS, 0, DUMMY_FILE_CONTENTS.length);
zip.closeArchiveEntry();
}
Path extractFolder = Paths.get("output_dir", "nested");
ProjectFilesystem filesystem =
TestProjectFilesystems.createProjectFilesystem(tmpFolder.getRoot());
ArchiveFormat.ZIP
.getUnarchiver()
.extractArchive(
zipFile,
filesystem,
extractFolder,
Optional.of(Paths.get("foo")),
ExistingFileMode.OVERWRITE_AND_CLEAN_DIRECTORIES);
assertFalse(filesystem.isDirectory(extractFolder.resolve("sibling")));
assertFalse(filesystem.isDirectory(extractFolder.resolve("foo")));
assertFalse(filesystem.isDirectory(extractFolder.resolve("some")));
Path bazDotTxtPath = extractFolder.resolve("bar").resolve("baz.txt");
Path bazDotShPath = extractFolder.resolve("bar").resolve("baz.sh");
assertTrue(filesystem.isDirectory(extractFolder.resolve("bar")));
assertTrue(filesystem.isFile(bazDotTxtPath));
assertTrue(filesystem.isFile(bazDotShPath));
assertTrue(filesystem.isExecutable(bazDotShPath));
assertEquals(new String(bazDotSh), filesystem.readFileIfItExists(bazDotShPath).get());
assertEquals(
new String(DUMMY_FILE_CONTENTS), filesystem.readFileIfItExists(bazDotTxtPath).get());
}