本文整理匯總了Java中org.apache.commons.compress.archivers.zip.ZipFile.close方法的典型用法代碼示例。如果您正苦於以下問題:Java ZipFile.close方法的具體用法?Java ZipFile.close怎麽用?Java ZipFile.close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.compress.archivers.zip.ZipFile
的用法示例。
在下文中一共展示了ZipFile.close方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: compareFiles
import org.apache.commons.compress.archivers.zip.ZipFile; //導入方法依賴的package包/類
/**
* Compares the content of two zip files. The zip files are considered equal, if
* the content of all zip entries is equal to the content of its corresponding entry
* in the other zip file.
*
* @author S3460
* @param zipSource the zip source
* @param resultZip the result zip
* @throws Exception the exception
*/
private void compareFiles(ZipFile zipSource, ZipFile resultZip) throws Exception {
boolean rc = false;
try {
for (Enumeration<ZipArchiveEntry> enumer = zipSource.getEntries(); enumer.hasMoreElements();) {
ZipArchiveEntry sourceEntry = enumer.nextElement();
ZipArchiveEntry resultEntry = resultZip.getEntry(sourceEntry.getName());
assertNotNull("Entry nicht generiert: " + sourceEntry.getName(), resultEntry);
byte[] oldBytes = toBytes(zipSource, sourceEntry);
byte[] newBytes = toBytes(resultZip, resultEntry);
rc = equal(oldBytes, newBytes);
assertTrue("bytes the same " + sourceEntry, rc);
}
} finally {
zipSource.close();
resultZip.close();
}
}
示例2: runJarPatcher
import org.apache.commons.compress.archivers.zip.ZipFile; //導入方法依賴的package包/類
/**
* Run jar patcher.
*
* @param originalName the original name
* @param targetName the target name
* @param originalZip the original zip
* @param newZip the new zip
* @param comparefiles the comparefiles
* @throws Exception the exception
*/
private void runJarPatcher(String originalName, String targetName, ZipFile originalZip, ZipFile newZip, boolean comparefiles) throws Exception {
try (ZipArchiveOutputStream output = new ZipArchiveOutputStream(new FileOutputStream(patchFile))) {
new JarDelta().computeDelta(originalName, targetName, originalZip, newZip, output);
}
ZipFile patch = new ZipFile(patchFile);
ZipArchiveEntry listEntry = patch.getEntry("META-INF/file.list");
if (listEntry == null) {
patch.close();
throw new IOException("Invalid patch - list entry 'META-INF/file.list' not found");
}
BufferedReader patchlist = new BufferedReader(new InputStreamReader(patch.getInputStream(listEntry)));
String next = patchlist.readLine();
String sourceName = next;
next = patchlist.readLine();
new JarPatcher(patchFile.getName(), sourceName).applyDelta(patch, new ZipFile(originalName), new ZipArchiveOutputStream(new FileOutputStream(resultFile)), patchlist);
if (comparefiles) {
compareFiles(new ZipFile(targetName), new ZipFile(resultFile));
}
}
示例3: testJarPatcherIdentFile
import org.apache.commons.compress.archivers.zip.ZipFile; //導入方法依賴的package包/類
/**
* Tests JarDelta and JarPatcher on two identical files.
*
* @throws Exception the exception
*/
@Test
public void testJarPatcherIdentFile() throws Exception {
ZipFile originalZip = makeSourceZipFile(sourceFile);
new JarDelta().computeDelta(sourceFile.getAbsolutePath(), sourceFile.getAbsolutePath(), originalZip, originalZip, new ZipArchiveOutputStream(new FileOutputStream(patchFile)));
ZipFile patch = new ZipFile(patchFile);
ZipArchiveEntry listEntry = patch.getEntry("META-INF/file.list");
if (listEntry == null) {
patch.close();
throw new IOException("Invalid patch - list entry 'META-INF/file.list' not found");
}
BufferedReader patchlist = new BufferedReader(new InputStreamReader(patch.getInputStream(listEntry)));
String next = patchlist.readLine();
String sourceName = next;
next = patchlist.readLine();
ZipFile source = new ZipFile(sourceFile);
new JarPatcher(patchFile.getName(), sourceName).applyDelta(patch, source, new ZipArchiveOutputStream(new FileOutputStream(resultFile)), patchlist);
compareFiles(new ZipFile(sourceFile), new ZipFile(resultFile));
}
示例4: testEmptyBaseDirRelativeDir
import org.apache.commons.compress.archivers.zip.ZipFile; //導入方法依賴的package包/類
@Test
public void testEmptyBaseDirRelativeDir() throws Exception {
String aTargetFilename = "target/Z6-input.zip";
ZipFilesTasklet aTasklet = new ZipFilesTasklet();
aTasklet.setSourceBaseDirectory(new FileSystemResource(""));
FileSystemResourcesFactory aSourceFactory = new FileSystemResourcesFactory();
aSourceFactory.setPattern("file:src/test/resources/testFiles/input.csv");
aTasklet.setSourceFactory(aSourceFactory );
ExpressionResourceFactory aDestinationFactory = new ExpressionResourceFactory();
aDestinationFactory.setExpression(aTargetFilename);
aTasklet.setDestinationFactory(aDestinationFactory );
assertEquals(RepeatStatus.FINISHED, aTasklet.execute(null, null));
assertTrue(new File(aTargetFilename).exists());
ZipFile aZipFile = new ZipFile(new File(aTargetFilename));
Enumeration<ZipArchiveEntry> aEntries = aZipFile.getEntries();
assertTrue(aEntries.hasMoreElements());
assertEquals("src/test/resources/testFiles/input.csv", aEntries.nextElement().getName());
assertFalse(aEntries.hasMoreElements());
aZipFile.close();
}
示例5: readFirstZipEntry
import org.apache.commons.compress.archivers.zip.ZipFile; //導入方法依賴的package包/類
/**
* Reads the first file entry in a zip file and returns it's contents
* as uncompressed byte-array
* @param zipFile the zip file to read from
* @return the first file entry (uncompressed)
* @throws IOException if there is an error accessing the zip file
*/
public static byte[] readFirstZipEntry(File zipFile) throws IOException {
// open zip
ZipFile zf = new ZipFile(zipFile);
Enumeration<ZipArchiveEntry> entries = zf.getEntries();
// read first entry to byte[]
ZipArchiveEntry entry = entries.nextElement();
InputStream is = zf.getInputStream(entry);
byte[] raw = ByteStreams.toByteArray(is);
// close all streams and return byte[]
is.close();
zf.close();
return raw;
}
示例6: isZipFile
import org.apache.commons.compress.archivers.zip.ZipFile; //導入方法依賴的package包/類
/**
* <p>
* isZipFile.
* </p>
*
* @param zipFile a {@link java.io.File} object.
* @return a boolean.
*/
public static boolean isZipFile(File zipFile) {
try {
ZipFile zf = new ZipFile(zipFile);
boolean isZip = zf.getEntries().hasMoreElements();
zf.close();
return isZip;
} catch (IOException e) {
return false;
}
}
示例7: isZipFile
import org.apache.commons.compress.archivers.zip.ZipFile; //導入方法依賴的package包/類
/**
* <p>
* isZipFile.
* </p>
*
* @param zipFile a {@link File} object.
* @return a boolean.
*/
public static boolean isZipFile(File zipFile) {
try {
ZipFile zf = new ZipFile(zipFile);
boolean isZip = zf.getEntries().hasMoreElements();
zf.close();
return isZip;
} catch (IOException e) {
return false;
}
}
示例8: fastSignInfo
import org.apache.commons.compress.archivers.zip.ZipFile; //導入方法依賴的package包/類
public static int fastSignInfo(File file) {
int v1 = SIGNED_NOT;
int v2 = SIGNED_NOT;
try {
ZipFile zip = new ZipFile(file);
if (zip.getEntry("META-INF/CERT.RSA") != null) {
v1 = SIGNED_V1;
}
Field archiveField = ZipFile.class.getDeclaredField("archive");
archiveField.setAccessible(true);
final RandomAccessFile archive = (RandomAccessFile) archiveField.get(zip);
Method positionAtCentralDirectory = ZipFile.class.getDeclaredMethod("positionAtCentralDirectory");
positionAtCentralDirectory.setAccessible(true);
positionAtCentralDirectory.invoke(zip);
long centralDirectoryOffset = archive.getFilePointer();
archive.seek(centralDirectoryOffset - 24);
byte[] buffer = new byte[24];
archive.readFully(buffer);
zip.close();
ByteBuffer footer = ByteBuffer.wrap(buffer);
footer.order(ByteOrder.LITTLE_ENDIAN);
if ((footer.getLong(8) == APK_SIG_BLOCK_MAGIC_LO)
&& (footer.getLong(16) == APK_SIG_BLOCK_MAGIC_HI)) {
v2 = SIGNED_V2;
}
} catch (IOException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) {
}
return v1 | v2;
}
示例9: unzipToTemporaryDirectory
import org.apache.commons.compress.archivers.zip.ZipFile; //導入方法依賴的package包/類
public static Path unzipToTemporaryDirectory(File file) throws IOException {
ZipFile zipFile = new ZipFile(file);
Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntries();
Path tempDirectory = Files.createTempDirectory("temp");
while (zipEntries.hasMoreElements()) {
ZipArchiveEntry entry = zipEntries.nextElement();
FileUtils.copyInputStreamToFile(zipFile.getInputStream(entry), new File(Paths.get(tempDirectory.toString(),"/" + entry.getName()).toString()));
}
zipFile.close();
return tempDirectory;
}
示例10: decompressZip
import org.apache.commons.compress.archivers.zip.ZipFile; //導入方法依賴的package包/類
public void decompressZip(File zipFile, String dir) throws IOException {
ZipFile zf = new ZipFile(zipFile);
try {
for (Enumeration<ZipArchiveEntry> entries = zf.getEntries(); entries
.hasMoreElements();) {
ZipArchiveEntry ze = entries.nextElement();
// 不存在則創建目標文件夾。
File targetFile = new File(dir, ze.getName());
// 遇到根目錄時跳過。
if (ze.getName().lastIndexOf("/") == (ze.getName().length() - 1)) {
continue;
}
// 如果文件夾不存在,創建文件夾。
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
InputStream i = zf.getInputStream(ze);
OutputStream o = null;
try {
o = new FileOutputStream(targetFile);
IOUtils.copy(i, o);
} finally {
if (i != null) {
i.close();
}
if (o != null) {
o.close();
}
}
}
} finally {
zf.close();
}
}
示例11: testZipFile
import org.apache.commons.compress.archivers.zip.ZipFile; //導入方法依賴的package包/類
@Test
public void testZipFile() throws Exception {
String aTargetFilename = "target/Z0-input.zip";
ZipFilesTasklet aTasklet = new ZipFilesTasklet();
aTasklet.setSourceBaseDirectory(new FileSystemResource("src/test/resources/testFiles/"));
FileSystemResourcesFactory aSourceFactory = new FileSystemResourcesFactory();
aSourceFactory.setPattern("file:src/test/resources/testFiles/input.csv");
aTasklet.setSourceFactory(aSourceFactory );
ExpressionResourceFactory aDestinationFactory = new ExpressionResourceFactory();
aDestinationFactory.setExpression(aTargetFilename);
aTasklet.setDestinationFactory(aDestinationFactory );
assertFalse(new File(aTargetFilename).exists());
StepContribution aStepContribution = mock(StepContribution.class);
assertEquals(RepeatStatus.FINISHED, aTasklet.execute(aStepContribution, null));
verify(aStepContribution, times(1)).incrementReadCount();
verify(aStepContribution, times(1)).incrementWriteCount(1);
assertTrue(new File(aTargetFilename).exists());
ZipFile aZipFile = new ZipFile(new File(aTargetFilename));
Enumeration<ZipArchiveEntry> aEntries = aZipFile.getEntries();
assertTrue(aEntries.hasMoreElements());
assertEquals("input.csv", aEntries.nextElement().getName());
assertFalse(aEntries.hasMoreElements());
aZipFile.close();
}
示例12: testZipTwoFile
import org.apache.commons.compress.archivers.zip.ZipFile; //導入方法依賴的package包/類
@Test
public void testZipTwoFile() throws Exception {
FileUtils.forceMkdir(new File("target/Z-testfiles/source"));
FileUtils.copyFile(new File("src/test/resources/testFiles/input.csv"), new File("target/Z-testfiles/source/CP0-input.csv"));
FileUtils.copyFile(new File("src/test/resources/testFiles/input.csv"), new File("target/Z-testfiles/source/CP1-input.csv"));
String aTargetFilename = "target/Z1-input.zip";
ZipFilesTasklet aTasklet = new ZipFilesTasklet();
aTasklet.setSourceBaseDirectory(new FileSystemResource("target/Z-testfiles/source/"));
FileSystemResourcesFactory aSourceFactory = new FileSystemResourcesFactory();
aSourceFactory.setPattern("file:target/Z-testfiles/source/*input*.csv");
aTasklet.setSourceFactory(aSourceFactory );
ExpressionResourceFactory aDestinationFactory = new ExpressionResourceFactory();
aDestinationFactory.setExpression(aTargetFilename);
aTasklet.setDestinationFactory(aDestinationFactory );
assertFalse(new File(aTargetFilename).exists());
StepContribution aStepContribution = mock(StepContribution.class);
assertEquals(RepeatStatus.FINISHED, aTasklet.execute(aStepContribution, null));
verify(aStepContribution, times(2)).incrementReadCount();
verify(aStepContribution, times(2)).incrementWriteCount(1);
assertTrue(new File(aTargetFilename).exists());
ZipFile aZipFile = new ZipFile(new File(aTargetFilename));
Enumeration<ZipArchiveEntry> aEntries = aZipFile.getEntries();
assertTrue(aEntries.hasMoreElements());
assertEquals("CP0-input.csv", aEntries.nextElement().getName());
assertTrue(aEntries.hasMoreElements());
assertEquals("CP1-input.csv", aEntries.nextElement().getName());
assertFalse(aEntries.hasMoreElements());
aZipFile.close();
}
示例13: testZipDirectory
import org.apache.commons.compress.archivers.zip.ZipFile; //導入方法依賴的package包/類
@Test
public void testZipDirectory() throws Exception {
FileUtils.forceMkdir(new File("target/Z-testfiles/source"));
FileUtils.copyFile(new File("src/test/resources/testFiles/input.csv"), new File("target/Z-testfiles/source/CP0-input.csv"));
FileUtils.copyFile(new File("src/test/resources/testFiles/input.csv"), new File("target/Z-testfiles/source/CP1-input.csv"));
String aTargetFilename = "target/Z2-input.zip";
ZipFilesTasklet aTasklet = new ZipFilesTasklet();
aTasklet.setSourceBaseDirectory(new FileSystemResource("target/Z-testfiles/"));
FileSystemResourcesFactory aSourceFactory = new FileSystemResourcesFactory();
aSourceFactory.setPattern("file:target/Z-testfiles/source/");
aTasklet.setSourceFactory(aSourceFactory );
ExpressionResourceFactory aDestinationFactory = new ExpressionResourceFactory();
aDestinationFactory.setExpression(aTargetFilename);
aTasklet.setDestinationFactory(aDestinationFactory );
assertFalse(new File(aTargetFilename).exists());
StepContribution aStepContribution = mock(StepContribution.class);
assertEquals(RepeatStatus.FINISHED, aTasklet.execute(aStepContribution, null));
verify(aStepContribution, times(3)).incrementReadCount();
verify(aStepContribution, times(3)).incrementWriteCount(1);
assertTrue(new File(aTargetFilename).exists());
ZipFile aZipFile = new ZipFile(new File(aTargetFilename));
Enumeration<ZipArchiveEntry> aEntries = aZipFile.getEntries();
assertTrue(aEntries.hasMoreElements());
assertEquals("source", aEntries.nextElement().getName());
assertTrue(aEntries.hasMoreElements());
assertEquals("source/CP0-input.csv", aEntries.nextElement().getName());
assertTrue(aEntries.hasMoreElements());
assertEquals("source/CP1-input.csv", aEntries.nextElement().getName());
assertFalse(aEntries.hasMoreElements());
aZipFile.close();
}
示例14: testZipDirectories
import org.apache.commons.compress.archivers.zip.ZipFile; //導入方法依賴的package包/類
@Test
public void testZipDirectories() throws Exception {
FileUtils.forceMkdir(new File("target/Z-testfiles/source/subdir"));
FileUtils.copyFile(new File("src/test/resources/testFiles/input.csv"), new File("target/Z-testfiles/source/CP0-input.csv"));
FileUtils.copyFile(new File("src/test/resources/testFiles/input.csv"), new File("target/Z-testfiles/source/subdir/CP1-input.csv"));
String aTargetFilename = "target/Z3-input.zip";
ZipFilesTasklet aTasklet = new ZipFilesTasklet();
aTasklet.setSourceBaseDirectory(new FileSystemResource("target/Z-testfiles/"));
FileSystemResourcesFactory aSourceFactory = new FileSystemResourcesFactory();
aSourceFactory.setPattern("file:target/Z-testfiles/source/");
aTasklet.setSourceFactory(aSourceFactory );
ExpressionResourceFactory aDestinationFactory = new ExpressionResourceFactory();
aDestinationFactory.setExpression(aTargetFilename);
aTasklet.setDestinationFactory(aDestinationFactory );
assertFalse(new File(aTargetFilename).exists());
StepContribution aStepContribution = mock(StepContribution.class);
assertEquals(RepeatStatus.FINISHED, aTasklet.execute(aStepContribution, null));
verify(aStepContribution, times(4)).incrementReadCount();
verify(aStepContribution, times(4)).incrementWriteCount(1);
assertTrue(new File(aTargetFilename).exists());
ZipFile aZipFile = new ZipFile(new File(aTargetFilename));
Enumeration<ZipArchiveEntry> aEntries = aZipFile.getEntries();
assertTrue(aEntries.hasMoreElements());
assertEquals("source", aEntries.nextElement().getName());
assertTrue(aEntries.hasMoreElements());
assertEquals("source/CP0-input.csv", aEntries.nextElement().getName());
assertTrue(aEntries.hasMoreElements());
assertEquals("source/subdir", aEntries.nextElement().getName());
assertTrue(aEntries.hasMoreElements());
assertEquals("source/subdir/CP1-input.csv", aEntries.nextElement().getName());
assertFalse(aEntries.hasMoreElements());
aZipFile.close();
}
示例15: testZipDirectoriesNotRecursive
import org.apache.commons.compress.archivers.zip.ZipFile; //導入方法依賴的package包/類
@Test
public void testZipDirectoriesNotRecursive() throws Exception {
FileUtils.forceMkdir(new File("target/Z-testfiles/source/subdir"));
FileUtils.copyFile(new File("src/test/resources/testFiles/input.csv"), new File("target/Z-testfiles/source/CP0-input.csv"));
FileUtils.copyFile(new File("src/test/resources/testFiles/input.csv"), new File("target/Z-testfiles/source/subdir/CP1-input.csv"));
String aTargetFilename = "target/Z4-input.zip";
ZipFilesTasklet aTasklet = new ZipFilesTasklet();
aTasklet.setSourceBaseDirectory(new FileSystemResource("target/Z-testfiles/"));
FileSystemResourcesFactory aSourceFactory = new FileSystemResourcesFactory();
aSourceFactory.setPattern("file:target/Z-testfiles/source/");
aTasklet.setSourceFactory(aSourceFactory );
ExpressionResourceFactory aDestinationFactory = new ExpressionResourceFactory();
aDestinationFactory.setExpression(aTargetFilename);
aTasklet.setDestinationFactory(aDestinationFactory );
aTasklet.setRecursive(false);
assertFalse(new File(aTargetFilename).exists());
StepContribution aStepContribution = mock(StepContribution.class);
assertEquals(RepeatStatus.FINISHED, aTasklet.execute(aStepContribution, null));
verify(aStepContribution, times(2)).incrementReadCount();
verify(aStepContribution, times(2)).incrementWriteCount(1);
assertTrue(new File(aTargetFilename).exists());
ZipFile aZipFile = new ZipFile(new File(aTargetFilename));
Enumeration<ZipArchiveEntry> aEntries = aZipFile.getEntries();
assertTrue(aEntries.hasMoreElements());
assertEquals("source", aEntries.nextElement().getName());
assertTrue(aEntries.hasMoreElements());
assertEquals("source/CP0-input.csv", aEntries.nextElement().getName());
assertFalse(aEntries.hasMoreElements());
aZipFile.close();
}