关于apache解压zip和sleep程序程序退出问题

本文探讨了使用Apache工具解压ZIP文件时遇到的问题:对于包含空文件夹的ZIP文件,如何正确识别并处理这些空文件夹。文章还介绍了通过检查文件名后缀来判断是否为目录的方法,并分享了一个关于线程控制的小技巧。

前两天写了

http://wcf1987.iteye.com/admin/blogs/894160

是利用apache解压zip包的,大体实验都很正常,但是后来发现如果解压缩的zip中含有空文件夹,会发生一个问题

  zipEntry = (ZipArchiveEntry) en.nextElement();    
                if (zipEntry.isDirectory()) {      
        // mkdir directory      
        String dirName = zipEntry.getName();   

 zipEntry是一个目录时(当zip中含有空目录时,zipEntry就会指向这个目录),但是zipEntry.isDirectory()却无法正常识别这个目录,他会认为这是一个文件....

   解决方案

String s=zipEntry.getName();
				if (s.endsWith(File.separator)) {   

 

从zipEntry中看到他的name属性中还可以辨识出这是一个目录(因为最后以/结尾),所以这部分代码修改为如上。。。后来解压正常

 

 

	/**
	 * stop the thread
	 */
	public void stopThread() {
		shutdownRequested = false;
		this.interrupt();
					

	}
	/**
	 * the thread sleep for reducing the system pressure
	 * 
	 * @param time
	 */
	private void sleepTime(int time) {

		try {	Thread.sleep(time * 1000);
		} catch (InterruptedException e) {
			return;
			// TODO Auto-generated catch block
			
		}

	}

 

这是这两天写的关于线程sleep,但是又需要外部调用停止的方法,简而言之,就是在stop方法中interrupt唤醒线程,在sleep方法中catch InterruptedException e,然后返回。

/* * Copyright 2010 Srikanth Reddy Lingala * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.lingala.zip4j.progress; /** * If Zip4j is set to run in thread mode, this class helps retrieve current progress */ public class ProgressMonitor { public enum State { READY, BUSY } public enum Result { SUCCESS, WORK_IN_PROGRESS, ERROR, CANCELLED } public enum Task { NONE, ADD_ENTRY, REMOVE_ENTRY, CALCULATE_CRC, EXTRACT_ENTRY, MERGE_ZIP_FILES, SET_COMMENT, RENAME_FILE} private State state; private long totalWork; private long workCompleted; private int percentDone; private Task currentTask; private String fileName; private Result result; private Exception exception; private boolean cancelAllTasks; private boolean pause; public ProgressMonitor() { reset(); } public void updateWorkCompleted(long workCompleted) { this.workCompleted += workCompleted; if (totalWork > 0) { percentDone = (int) ((this.workCompleted * 100 / totalWork)); if (percentDone > 100) { percentDone = 100; } } while (pause) { try { Thread.sleep(150); } catch (InterruptedException e) { //Do nothing } } } public void endProgressMonitor() { result = Result.SUCCESS; percentDone = 100; reset(); } public void endProgressMonitor(Exception e) { result = Result.ERROR; exception = e; reset(); } public void fullReset() { reset(); fileName = null; totalWork = 0; workCompleted = 0; percentDone = 0; } private void reset() { currentTask = Task.NONE; state = State.READY; } public State getState() { return state; } public void setState(State state) { this.state = state; } public long getTotalWork() { return totalWork; } public void setTotalWork(long totalWork) { this.totalWork = totalWork; } public long getWorkCompleted() { return workCompleted; } public int getPercentDone() { return percentDone; } public void setPercentDone(int percentDone) { this.percentDone = percentDone; } public Task getCurrentTask() { return currentTask; } public void setCurrentTask(Task currentTask) { this.currentTask = currentTask; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public Result getResult() { return result; } public void setResult(Result result) { this.result = result; } public Exception getException() { return exception; } public void setException(Exception exception) { this.exception = exception; } public boolean isCancelAllTasks() { return cancelAllTasks; } public void setCancelAllTasks(boolean cancelAllTasks) { this.cancelAllTasks = cancelAllTasks; } public boolean isPause() { return pause; } public void setPause(boolean pause) { this.pause = pause; } }
最新发布
08-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值