本文整理汇总了Java中java.util.zip.ZipOutputStream.finish方法的典型用法代码示例。如果您正苦于以下问题:Java ZipOutputStream.finish方法的具体用法?Java ZipOutputStream.finish怎么用?Java ZipOutputStream.finish使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.zip.ZipOutputStream
的用法示例。
在下文中一共展示了ZipOutputStream.finish方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: zipDirectory
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
* Zips an entire directory specified by the path.
*
* @param sourceDirectory the directory to read from. This directory and all
* subdirectories will be added to the zip-file. The path within the zip
* file is relative to the directory given as parameter, not absolute.
* @param outputStream the stream to write the zip-file to. This method does not close
* outputStream.
* @throws IOException the zipping failed, e.g. because the input was not
* readable.
*/
static void zipDirectory(
File sourceDirectory,
OutputStream outputStream) throws IOException {
checkNotNull(sourceDirectory);
checkNotNull(outputStream);
checkArgument(
sourceDirectory.isDirectory(),
"%s is not a valid directory",
sourceDirectory.getAbsolutePath());
ZipOutputStream zos = new ZipOutputStream(outputStream);
for (File file : sourceDirectory.listFiles()) {
zipDirectoryInternal(file, "", zos);
}
zos.finish();
}
示例2: write
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
@Override
public boolean write(OutputStream out, ProgressReporter progressReporter) throws SerializerException {
if (getMode() == Mode.BODY) {
try {
ZipOutputStream zipOutputStream = new ZipOutputStream(out);
zipOutputStream.putNextEntry(new ZipEntry("doc.kml"));
writeKmlFile(zipOutputStream);
zipOutputStream.closeEntry();
zipOutputStream.putNextEntry(new ZipEntry("files/collada.dae"));
ifcToCollada.writeToOutputStream(zipOutputStream, progressReporter);
zipOutputStream.closeEntry();
zipOutputStream.finish();
zipOutputStream.flush();
} catch (IOException e) {
LOGGER.error("", e);
}
setMode(Mode.FINISHED);
return true;
} else if (getMode() == Mode.HEADER) {
setMode(Mode.BODY);
return true;
} else if (getMode() == Mode.FINISHED) {
return false;
}
return false;
}
示例3: main
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
URLConnection conn = B7050028.class.getResource("B7050028.class").openConnection();
int len = conn.getContentLength();
byte[] data = new byte[len];
InputStream is = conn.getInputStream();
is.read(data);
is.close();
conn.setDefaultUseCaches(false);
File jar = File.createTempFile("B7050028", ".jar");
jar.deleteOnExit();
OutputStream os = new FileOutputStream(jar);
ZipOutputStream zos = new ZipOutputStream(os);
ZipEntry ze = new ZipEntry("B7050028.class");
ze.setMethod(ZipEntry.STORED);
ze.setSize(len);
CRC32 crc = new CRC32();
crc.update(data);
ze.setCrc(crc.getValue());
zos.putNextEntry(ze);
zos.write(data, 0, len);
zos.closeEntry();
zos.finish();
zos.close();
os.close();
System.out.println(new URLClassLoader(new URL[] {new URL("jar:" + jar.toURI() + "!/")}, ClassLoader.getSystemClassLoader().getParent()).loadClass(B7050028.class.getName()));
}
示例4: createFakeJAR
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
private void createFakeJAR(File f, String content) throws IOException {
// create just enough to make URLMapper recognize file as JAR:
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(f));
writeZipFileEntry(zos, content, "some content".getBytes());
zos.finish();
zos.close();
}
示例5: testUnZip
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
@Test (timeout = 30000)
public void testUnZip() throws IOException {
// make sa simple zip
setupDirs();
// make a simple tar:
final File simpleZip = new File(del, FILE);
OutputStream os = new FileOutputStream(simpleZip);
ZipOutputStream tos = new ZipOutputStream(os);
try {
ZipEntry ze = new ZipEntry("foo");
byte[] data = "some-content".getBytes("UTF-8");
ze.setSize(data.length);
tos.putNextEntry(ze);
tos.write(data);
tos.closeEntry();
tos.flush();
tos.finish();
} finally {
tos.close();
}
// successfully untar it into an existing dir:
FileUtil.unZip(simpleZip, tmp);
// check result:
assertTrue(new File(tmp, "foo").exists());
assertEquals(12, new File(tmp, "foo").length());
final File regularFile = new File(tmp, "QuickBrownFoxJumpsOverTheLazyDog");
regularFile.createNewFile();
assertTrue(regularFile.exists());
try {
FileUtil.unZip(simpleZip, regularFile);
assertTrue("An IOException expected.", false);
} catch (IOException ioe) {
// okay
}
}
示例6: zipTheDirectory
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
private void zipTheDirectory(OutputStream outputStream, Path writeDirectory) throws IOException {
// Create the archive.
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
// Copy the files into the ZIP file.
for (Path f : PathUtils.list(writeDirectory)) {
addToZipFile(f, zipOutputStream);
}
// Push the data into the parent stream (gets returned to the server).
zipOutputStream.finish();
zipOutputStream.flush();
}
示例7: ZipFiles
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
public static void ZipFiles(List<String> filepaths, String zipFileString)
throws Exception
{
File zipFile = new File(zipFileString);
if (zipFile != null && zipFile.exists())
{
zipFile.delete();
}
if (filepaths != null)
{
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString));
for (String filepath : filepaths)
{
File file = new File(filepath);
if (file != null && file.exists())
{
ZipEntry zipEntry = new ZipEntry(file.getName());
FileInputStream inputStream = new FileInputStream(file);
outZip.putNextEntry(zipEntry);
int len;
byte[] buffer = new byte[4096];
while ((len = inputStream.read(buffer)) != -1)
{
outZip.write(buffer, 0, len);
}
outZip.closeEntry();
}
}
outZip.finish();
outZip.close();
}
}
示例8: sanitize
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
@Override
public void sanitize(InputStream inputStream, OutputStream outputStream, BleachSession session) throws BleachException {
ZipInputStream zipIn = new ZipInputStream(inputStream);
ZipOutputStream zipOut = new ZipOutputStream(outputStream);
try {
ZipEntry entry;
while ((entry = zipIn.getNextEntry()) != null) {
LOGGER.trace("Entry: {} - Size: (original: {}, compressed: {})",
entry.getName(),
entry.getSize(),
entry.getCompressedSize());
if (entry.isDirectory()) {
ZipEntry newEntry = new ZipEntry(entry);
zipOut.putNextEntry(newEntry);
} else {
sanitizeFile(session, zipIn, zipOut, entry);
}
zipOut.closeEntry();
}
zipOut.finish();
} catch (IOException e) {
LOGGER.error("Error in ArchiveBleach", e);
}
}
示例9: clearBackupState
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
private int clearBackupState(boolean closeFile) {
if (backupState == null) {
return TRANSPORT_OK;
}
try {
IoUtils.closeQuietly(backupState.getInputFileDescriptor());
backupState.setInputFileDescriptor(null);
ZipOutputStream outputStream = backupState.getOutputStream();
if (outputStream != null) {
outputStream.closeEntry();
}
if (backupState.getPackageIndex() == configuration.getPackageCount() || closeFile) {
if (outputStream != null) {
outputStream.finish();
outputStream.close();
}
IoUtils.closeQuietly(backupState.getOutputFileDescriptor());
backupState = null;
}
} catch (IOException ex) {
Log.e(TAG, "Error cancelling full backup: ", ex);
return TRANSPORT_ERROR;
}
return TRANSPORT_OK;
}
示例10: unzip1
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
@Test
public void unzip1() throws Exception {
final FileSystem fs = Jimfs.newFileSystem();
final String expected = "Hello, world. ";
// Write a test file.
Files.write(fs.getPath("hello.txt"), expected.getBytes());
// Create the zip.
{
final ZipOutputStream zipOutputStream = new ZipOutputStream(
Files.newOutputStream(fs.getPath("stuff.zip")));
zipOutputStream.finish();
zipOutputStream.close();
}
// Add the test file to the zip.
try (final FileSystem zipFileSystem = EvenMoreFiles.zipFileSystem(fs.getPath("stuff.zip"))) {
Files.copy(fs.getPath("hello.txt"), zipFileSystem.getPath("message.txt"),
StandardCopyOption.REPLACE_EXISTING);
}
// Unpack the zip!
EvenMoreFiles.unzip(fs.getPath("stuff.zip"), fs.getPath("stuff"), Optional.empty());
// Verify the unzipped file matches the test file
final byte[] bytes = Files.readAllBytes(fs.getPath("stuff", "message.txt"));
final String actual = Charset.defaultCharset().decode(ByteBuffer.wrap(bytes)).toString();
assertEquals(expected, actual);
}
示例11: unzip2
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
@Test
public void unzip2() throws Exception {
final FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
final String expected = "Hello, world. ";
// Write a test file.
Files.write(fs.getPath("hello.txt"), expected.getBytes());
// Create the zip.
{
final ZipOutputStream zipOutputStream = new ZipOutputStream(
Files.newOutputStream(fs.getPath("stuff.zip")));
zipOutputStream.finish();
zipOutputStream.close();
}
// Add the test file to the zip.
try (final FileSystem zipFileSystem = EvenMoreFiles.zipFileSystem(fs.getPath("stuff.zip"))) {
Files.createDirectories(zipFileSystem.getPath("a", "b", "c"));
Files.copy(fs.getPath("hello.txt"), zipFileSystem.getPath("a", "b", "c", "message.txt"),
StandardCopyOption.REPLACE_EXISTING);
}
// Unpack the zip!
EvenMoreFiles.unzip(
fs.getPath("stuff.zip"),
fs.getPath("stuff"),
Optional.of(fs.getPath("a", "b").toString()));
// Verify the unzipped file matches the test file
final byte[] bytes = Files.readAllBytes(fs.getPath("stuff", "c", "message.txt"));
final String actual = Charset.defaultCharset().decode(ByteBuffer.wrap(bytes)).toString();
assertEquals(expected, actual);
}
示例12: build
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
@org.netbeans.api.annotations.common.SuppressWarnings("OS_OPEN_STREAM")
@Messages({
"# {0} - ZIP file", "MSG_building=Building {0}",
"# {0} - ZIP entry name", "MSG_packed=Packed: {0}"
})
private static boolean build(File root, File zip) throws IOException {
final AtomicBoolean canceled = new AtomicBoolean();
ProgressHandle handle = ProgressHandleFactory.createHandle(MSG_building(zip.getName()), new Cancellable() {
@Override public boolean cancel() {
return canceled.compareAndSet(false, true);
}
});
handle.start();
try {
List<String> files = new ArrayList<String>();
scanForFiles(root, files, "", handle, canceled, true);
if (canceled.get()) {
return false;
}
handle.switchToDeterminate(files.size());
OutputStream os = new FileOutputStream(zip);
try {
ZipOutputStream zos = new ZipOutputStream(os);
Set<String> written = new HashSet<String>();
String prefix = root.getName() + '/';
for (int i = 0; i < files.size(); i++) {
if (canceled.get()) {
return false;
}
String name = files.get(i);
writeEntry(prefix + name, written, zos, new File(root, name));
handle.progress(MSG_packed(name), i);
}
zos.finish();
zos.close();
} finally {
os.close();
}
} finally {
handle.finish();
}
return true;
}
示例13: compressFile
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
public static void compressFile(String srcFilePath, String zipFilePath) {
Throwable e;
Throwable th;
if (!StringUtils.isEmpty(srcFilePath) && !StringUtils.isEmpty(zipFilePath)) {
File srcFile = new File(srcFilePath);
if (srcFile.exists()) {
ZipOutputStream zipOutputStream = null;
try {
ZipOutputStream zipOutputStream2 = new ZipOutputStream(new FileOutputStream(zipFilePath));
try {
compressFiles(srcFile.getParent() + File.separator, srcFile.getName(), zipOutputStream2);
if (zipOutputStream2 != null) {
try {
zipOutputStream2.finish();
zipOutputStream2.close();
zipOutputStream = zipOutputStream2;
return;
} catch (Throwable e2) {
LogTool.e(TAG, "", e2);
}
}
zipOutputStream = zipOutputStream2;
} catch (Exception e3) {
e2 = e3;
zipOutputStream = zipOutputStream2;
try {
LogTool.e(TAG, "", e2);
if (zipOutputStream != null) {
try {
zipOutputStream.finish();
zipOutputStream.close();
} catch (Throwable e22) {
LogTool.e(TAG, "", e22);
}
}
} catch (Throwable th2) {
th = th2;
if (zipOutputStream != null) {
try {
zipOutputStream.finish();
zipOutputStream.close();
} catch (Throwable e222) {
LogTool.e(TAG, "", e222);
}
}
throw th;
}
} catch (Throwable th3) {
th = th3;
zipOutputStream = zipOutputStream2;
if (zipOutputStream != null) {
zipOutputStream.finish();
zipOutputStream.close();
}
throw th;
}
} catch (Exception e4) {
e222 = e4;
LogTool.e(TAG, "", e222);
if (zipOutputStream != null) {
zipOutputStream.finish();
zipOutputStream.close();
}
}
}
}
}
示例14: ZipFolder
import java.util.zip.ZipOutputStream; //导入方法依赖的package包/类
/**
* Compress file and folder
*
* @param srcFileString file or folder to be Compress
* @param zipFileString the path name of result ZIP
* @throws Exception
*/
public static void ZipFolder(String srcFileString, String zipFileString)
throws Exception
{
File zipFile = new File(zipFileString);
if (zipFile != null && zipFile.exists())
{
zipFile.delete();
}
// create ZIP
// create the file
File file = new File(srcFileString);
if (file != null && file.exists() && file.isDirectory())
{
File[] files = file.listFiles();
if (files != null && files.length > 0)
{
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString));
for (int i = 0; i < files.length; i++)
{
File fileItem = files[i];
if (fileItem != null && fileItem.exists() && fileItem.isFile())
{
ZipEntry zipEntry = new ZipEntry(fileItem.getName());
FileInputStream inputStream = new FileInputStream(fileItem);
outZip.putNextEntry(zipEntry);
int len;
byte[] buffer = new byte[4096];
while ((len = inputStream.read(buffer)) != -1)
{
outZip.write(buffer, 0, len);
}
outZip.closeEntry();
}
}
outZip.finish();
outZip.close();
}
}
}